hadrienj
hadrienj

Reputation: 2003

import subset of large table by name in R

I have a very large table in a .txt file and I'd like to import only certain rows in R. For exemple, with a table like this:

Name    Time    Mesure
Bob     17:24   0.418
Jimmy   15:30   0.436
Charles 05:26   0.257

I know it is possible to choose the rows to import by number indexing with :

table1 <- read.table(table0, skip=1, nrow=1, header=FALSE, sep="")

which gives :

V1      V2      V3
Jimmy   15:30   0.436

But how can I obtain the rows in which the value of 'Name' is 'Jimmy' without knowing the index ?

Upvotes: 0

Views: 97

Answers (1)

akrun
akrun

Reputation: 887831

Try

read.table(pipe('grep "Jimmy" "table1.txt"')) #table1.txt is the file
#    V1    V2    V3
#1 Jimmy 15:30 0.436

Update

To use multiple words, try

read.table(pipe('grep "Bob\\|Jimmy" "table1.txt"'))
#     V1    V2    V3
#1   Bob 17:24 0.418
#2 Jimmy 15:30 0.436

Upvotes: 2

Related Questions