[R] How to read only specified columns from a data file

David Winsemius dwinsemius at comcast.net
Tue Mar 15 18:15:48 CET 2011


On Mar 15, 2011, at 1:11 PM, <rex.dwyer at syngenta.com> wrote:

> I think you need to read an introduction to R.
> For starters, read.table returns its results as a value, which you  
> are not saving.
> The probable answer to your question:
> Read the whole file with read.table, and select columns you need,  
> e.g.:
> tab <- read.table(myfile, skip=2)[,1:5]
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org 
> ] On Behalf Of Luis Ridao
> Sent: Tuesday, March 15, 2011 11:53 AM
> To: r-help at r-project.org
> Subject: [R] How to read only specified columns from a data file
>
> R-help,
>
> I'm trying to read a data file with plenty of columns.
> I just need the first 5 but it doe not work by doing something like:
>
>> mycols <- rep(NULL, 430) ; mycols[c(1:4)] <- NA
>> read.table(myfile, skip=2, colClasses=mycols)

I would have suggested:

mycols <- rep(NULL, 430) ; mycols[1:5] <- rep("numeric", 5)
inp <- read.table(myfile, skip=2, colClasses=mycols)
head(inp)

-- 
David.

>
> Any suggestions?
>
> Thanks in advance
>
> ______________________________________________
> 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.
>
>
>
>
> message may contain confidential information. If you are not the  
> designated recipient, please notify the sender immediately, and  
> delete the original and any copies. Any use of the message by you is  
> prohibited.
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list