[R] [newbie] read row from file into vector
Greg.Snow at imail.org
Thu Dec 29 23:03:41 CET 2011
The scan function can be used to read a single row. If your file has multiple rows you can use the skip and nlines arguments to determine which row to read. With the what argument sent to a single item (a number or string depending on which you want) it will read each element on that row into a vector.
If you want to do more of the hard work yourself you can read in a whole line as a single string using the readLines function then use the strsplit (or possibly better, tools from the gsubfun package) to split that string into a vector (the unlist function may also be of help).
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
greg.snow at imail.org
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Tom Roche
> Sent: Thursday, December 29, 2011 1:51 PM
> To: r-help at r-project.org
> Subject: [R] [newbie] read row from file into vector
> summary: how to read a row (not column) from a file into a vector (not
> a data frame)?
> I'm using
> $ lsb_release -ds
> Linux Mint Debian Edition
> $ uname -rv
> 3.0.0-1-amd64 #1 SMP Sun Jul 24 02:24:44 UTC 2011
> $ R --version
> R version 2.14.1 (2011-12-22)
> I'm new to R (having previously used it only for graphics), but have
> worked in many other languages. I've got a CSV file from which I'd like
> to read the values from a single *row* into a vector. E.g., for a file
> such that
> $ head -n 2 ~/data/foo.csv | tail -n 1
> I'd like to be able to populate a vector 'v' s.t. v=5718, ...
> I can't seem to do that with, e.g., read.csv(...) or scan(...), both of
> which seem column-oriented. What am I missing?
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help