Ashoka
Ashoka

Reputation: 151

How to delete all rows in R until a certain value

I have a several data frames which start with a bit of text. Sometimes the information I need starts at row 11 and sometimes it starts at row 16 for instance. It changes. All the data frames have in common that the usefull information starts after a row with the title "location".

I'd like to make a loop to delete all the rows in the data frame above the useful information (including the row with "location").

Upvotes: 4

Views: 796

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226192

I'm guessing that you want something like this:

readfun <- function(fn,n=-1,target="location",...) {
   r <- readLines(fn,n=n)
   locline <- grep(target,r)[1]
   read.table(fn,skip=locline,...)
}

This is fairly inefficient because it reads the data file twice (once as raw character strings and once as a data frame), but it should work reasonably well if your files are not too big. (@MrFlick points out in the comments that if you have a reasonable upper bound on how far into the file your target will occur, you can set n so that you don't have to read the whole file just to search for the target.)

I don't know any other details of your files, but it might be safer to use "^location" to identify a line that begins with that string, or some other more specific target ...

Upvotes: 2

Related Questions