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

Tony Plate tplate at blackmesacapital.com
Tue Aug 10 22:05:06 CEST 2004


At Tuesday 01:55 PM 8/10/2004, F Duan wrote:
>Thanks a lot.
>
>Your way works perfect. And one more tiny question related to your codes:
>
>My data file has many columns to be omitted (suppose the first 20 ones), 
>but I
>found "scan(myfile, what=list(rep(NULL, 20), rep(0, 5))" doesn't work. I 
>had to
>to type "NULL" 20 times and "0" five times in the "list(...)".

That's because rep(NULL, 20) returns a single NULL -- it's not obvious what 
else it could sensibly return.  What you need to do is replicate 20 times a 
list containing NULL (and a list containing NULL is quite a different 
object to NULL).  E.g.:

 > rep(NULL, 20)
NULL
 > c(rep(list(NULL), 3), rep(list(0), 2))
[[1]]:
NULL

[[2]]:
NULL

[[3]]:
NULL

[[4]]:
[1] 0

[[5]]:
[1] 0

 >

Tony Plate



>But anyway, it works and saves a lot of memory for me. Thank you again.
>
>Frank
>
>
>Quoting Gabor Grothendieck <ggrothendieck at myway.com>:
>
> > Gabor Grothendieck <ggrothendieck <at> myway.com> writes:
> >
> > :
> > : 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:
> > :
> >
> > oops. That should be 1 and 3.
> >
> > :    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))
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
> >
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list