How do I manipulate the read.table function to read in only the 2nd column??? [[alternative HTML version deleted]]
"mark salsburg" <mark.salsburg at gmail.com> writes:> How do I manipulate the read.table function to read in only the 2nd > column???If your data is small, you can read in all columns and then subset the resulting data frame. Try that first. Perhaps there is a nicer way to do this that I don't know about, but recently I coded up the following to allow for a "streamy" read.table. I've adjusted a few things, but haven't tested. May not work as is, but it should give you an idea. + seth readBatch <- function(con, batch.size) { colClasses <- rep("character", 20) ## fix for your data ## adjust to pick out the columns that you want read.csv(con, colClasses=colClasses, as.is=TRUE, nrows=batch.size, header=FALSE)[, 1:2] } readTableStreamily <- function(filePath) { BATCH_SIZE <- 5000 ## no idea what a good value is depends on file and RAM con <- file(filePath, 'r') colNames <- readBatch(con, batch.size=1) chunks <- list() i <- 1 done <- FALSE while (!done) { done <- tryCatch({ cat(".") chunks[[i]] <- readBatch(con, batch.size=BATCH_SIZE) i <- i + 1 FALSE }, error=function(e) TRUE) } close(con) cat("\n") df <- do.call("rbind", chunks) names(df) <- colNames df }
---------- Forwarded message ---------- From: mark salsburg <mark.salsburg@gmail.com> Date: Mar 7, 2006 4:57 PM Subject: Re: [R] reading in only one column from text file To: Berton Gunter <gunter.berton@gene.com> I've tried that: read.table(myData, colClasses = NULL) colClasses doesn't seem to do anything when I put in NULL. How do I tell R to skip the 2nd column i'm reading in??? thank you, On 3/7/06, Berton Gunter <gunter.berton@gene.com> wrote:> > See the "NULL" value for argument colClasses of read.table(). > > -- Bert Gunter > Genentech Non-Clinical Statistics > South San Francisco, CA > > "The business of the statistician is to catalyze the scientific learning > process." - George E. P. Box > > > > > -----Original Message----- > > From: r-help-bounces@stat.math.ethz.ch > > [mailto: r-help-bounces@stat.math.ethz.ch] On Behalf Of mark salsburg > > Sent: Tuesday, March 07, 2006 1:30 PM > > To: R-help@stat.math.ethz.ch > > Cc: r-help@stat.math.ethz.ch > > Subject: [R] reading in only one column from text file > > > > How do I manipulate the read.table function to read in only the 2nd > > column??? > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@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 > > > >[[alternative HTML version deleted]]
"mark salsburg" <mark.salsburg at gmail.com> writes:> How do I manipulate the read.table function to read in only the 2nd > column???Something along the lines of cc <- rep("NULL", ncols) cc[2] <- NA # use type.convert read.table(.... colClasses=cc ....) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Kjetil Brinchmann Halvorsen
2006-Mar-07 22:06 UTC
[R] reading in only one column from text file
mark salsburg wrote:> How do I manipulate the read.table function to read in only the 2nd > column???Se the colClasses argument of read.table() Kjetil> > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >
You are not reading the Help file correctly. It says:
"Character.A vector of classes to be assumed for the columns. Recycled as
necessary."
^^^^^^^^^^^^^^^^
colClasses=NULL) means that you have no colClasses argument. It does exactly
what you tell it to (use whatever defaults it has, inother words).
c("numeric","NULL",...)) ## however many more columns you
have for ...
Please note the **quotes** . It behaves as documented.
-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
> -----Original Message-----
> From: mark salsburg [mailto:mark.salsburg at gmail.com]
> Sent: Tuesday, March 07, 2006 1:57 PM
> To: Berton Gunter
> Subject: Re: [R] reading in only one column from text file
>
> I've tried that:
>
> read.table(myData, colClasses = NULL)
>
> colClasses doesn't seem to do anything when I put in NULL.
>
> How do I tell R to skip the 2nd column i'm reading in???
>
> thank you,
>
>
>
>
> On 3/7/06, Berton Gunter <gunter.berton at gene.com> wrote:
>
> See the "NULL" value for argument colClasses of read.table().
>
> -- Bert Gunter
> Genentech Non-Clinical Statistics
> South San Francisco, CA
>
> "The business of the statistician is to catalyze the
> scientific learning
> process." - George E. P. Box
>
>
>
> > -----Original Message-----
> > From: r-help-bounces at stat.math.ethz.ch
> > [mailto: r-help-bounces at stat.math.ethz.ch
> <mailto:r-help-bounces at stat.math.ethz.ch> ] On Behalf Of mark
salsburg
> > Sent: Tuesday, March 07, 2006 1:30 PM
> > To: R-help at stat.math.ethz.ch
> > Cc: r-help at stat.math.ethz.ch
> <mailto:r-help at stat.math.ethz.ch>
> > Subject: [R] reading in only one column from text file
> >
> > How do I manipulate the read.table function to read
> in only the 2nd
> > column???
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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
> >
>
>
>
>
>
read.table("datafile", colClasses=c("NULL",
"numeric"), ...)
or something like that.
Andy
From: mark salsburg>
> How do I manipulate the read.table function to read in only
> the 2nd column???
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
>
>
You might want to read ?scan and pay attention to what= argument Jean mark salsburg wrote:>How do I manipulate the read.table function to read in only the 2nd >column??? > > [[alternative HTML version deleted]] > >______________________________________________ >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 > > >