[R] How to exclude lines that match certain regex when using read.table?

Sharpie chuck at sharpsteen.net
Fri Dec 4 04:09:13 CET 2009



pengyu.ut wrote:
> 
> I'm thinking of using external program 'grep' and pipe() to do so. But
> I'm wondering if there is a more efficient way to do so purely in R
> 

I would just suck the whole table in using read.table(), locate the lines
that I don't want using apply() and grepl() and then reduce the data set:

  dataSet <- read.table( "someData.txt" )

  dataToDrop <- apply( dataSet, 1, function( row ){

    return(
      any( grepl( "regex", row ) )
    )

  })

  dataSet <- subset( dataSet, !dataToDrop )

Since this solution executes entirely in R without resorting to system()
calls, it should be portable between platforms.

-Charlie
-- 
View this message in context: http://n4.nabble.com/How-to-exclude-lines-that-match-certain-regex-when-using-read-table-tp948207p948221.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list