[R] how do I read only specific columns using read.csv or other read function

Charles C. Berry cberry at tajo.ucsd.edu
Wed Jul 2 19:13:08 CEST 2008


On Wed, 2 Jul 2008, Ben Tupper wrote:

>
> On Jul 2, 2008, at 6:53 AM, Philip James Smith wrote:
>
>> Hi R people:
>> 
>> I have huge files with as many as 5000 columns. I'd really like to read 
>> only certain columns of those files. I know column names I want to read.
>> 
>> I looked at the documentation of read.csv . Although there is a col.names 
>> option, it allows users to specify the names of the columns, rather than to 
>> pick the columns of interest.
>> 
>> Any suggestions on how to pick the columns I want to read only, rather than 
>> the entire file, would be greatly appreciated.


There is a unix utility called 'cut' that enables stuff like

    columns.1.3.5.to.7 <- read.csv( pipe( "cut -d, -f1,3,5-7 your.file" ) )

and using

 	col.pos <- match(names.of.variables.you.want,
 			 scan("your.file", what=character(0), nlines=1 )

will enable you to set up the call to pipe.

HTH,

Chuck

>> 
>
> Hello,
>
> I think you want explicitly set the colClasses argument such that the columns 
> you *don't* want are set to NULL and all others are set to appropriate 
> classes.
>
> Cheers,
> Ben
>
>
>
>
>
>
>> Phil Smith
>> Duluth, GA
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> Ben Tupper
> PemaquidRiver at tidewater.net
>
> I GoodSearch for Ashwood Waldorf School.
>
> Raise money for your favorite charity or school just by searching the 
> Internet with GoodSearch - www.goodsearch.com - powered by Yahoo!
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list