Reputation: 79
I need to extract data from a raw file using R.There is some useless data before and the actual table starts from lets say 100th line.Now reading it from that line can be done using read.csv(skip=99) but i want to make it dynamic by automatically start reading from that line since there are multiple files with the actual table starting from different lines. The first line of the useful data will start from String "Time".
Any help will be appreciated. Thanks
Upvotes: 0
Views: 486
Reputation: 6750
Use readLines
to get the text file as a character vector.
Then find a line starting with "Time" and discard elements before that.
You can now make the remaining into data frame by read.csv(text=)
.
Here is an example.
# make a dummy file
write("junk\njunk\nTime,x\n1,2\n3,4", "tmp.csv")
x <- readLines("tmp.csv")
i <- grep("^Time", x)
x <- x[i:length(x)]
read.csv(text=x)
Upvotes: 2