[R] How to import specific column(s) using "read.table"?

Gabor Grothendieck ggrothendieck at myway.com
Tue Aug 10 07:09:06 CEST 2004


F Duan <f.duan <at> yale.edu> writes:

> I have a very big tab-delim txt file with header and I only want to import 
> several columns into R. I checked the options for Â"read.tableÂ" and only 

Try using scan with the what=list(...) and flush=TRUE arguments.  
For example, if your data looks like this:

1 2 3 4 
5 6 7 8 
9 10 11 12
13 14 15 16

then you could read columns 2 and 4 into a list with:

   scan(myfile, what = list(0, NULL, 0), flush = TRUE)

or read in and convert to a data frame via:

   do.call("cbind", scan(myfile, what = list(0, NULL, 0), flush = TRUE))




More information about the R-help mailing list