开发者

R: how to read in a series of txt files to R as csv but firstly delete the first three lines of the txt files

I have a series of txt files.

I have all these filenames in a csv file X in the Column V1 (X$V1), e.g. a.txt, b.txt....

The text files are in the format as follows:

title:xxx
date:xxx
person-in-charge:xxx

sx2sa 333 444 666 ggge4
xatak eees 566 6763 gaeta

What I want is to get a data.frame as follows with R.

filename  column3 column5
a         444     ggge4
b         566     gaeta

By learning from the other answers in stackoverflow:

I guess I can use scan to readin txt files. however, after using scan, how can I read in the remaining txt as csv separated by tag?

I guess I may use read.table or read.csv, and delete the remaining text afterwards, however, I found I cannot get it done, as the first 3 lines of the txt cannot be re开发者_StackOverflow社区cognized by the csv or table format, all the items are now readin and put into the first row of the dataframe. I used the command lines as follow, is it the the source of the problem:

x <- read.csv (file="a.txt", header=FALSE)
x <- read.table (file="a.txt", header=FALSE)

Did I miss any important tools in R? I believe there are smart way to do all these efficiently with R, could experts in the field give me some more comments or instructions? Thanks a lot.


You could use the skip parameter, like so:

> read.csv('a.txt', header=FALSE, skip=4, sep=' ')
     V1   V2  V3   V4    V5
1 sx2sa  333 444  666 ggge4
2 xatak eees 566 6763 gaeta

To combine data from multiple files, you could use rbind to stack the data frame on top of each other.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜