[R] [newbie] read row from file into vector

Daniel Nordlund djnordlund at frontier.com
Fri Dec 30 00:36:10 CET 2011


> -----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 2:08 PM
> To: r-help at r-project.org
> Subject: Re: [R] [newbie] read row from file into vector
> 
> 
> Tom Roche 11-12-29 3:51 PM
> >>> E.g., for a file such that
> 
> >>> $ head -n 2 ~/data/foo.csv | tail -n 1
> >>>
> 5718,0.3,0.47,0,0,0,0,0,0,0,0,0.08,0.37,0,0,0.83,1.55,0,0,0,0,0,0,0,0,0,0.
> 00,2.48,2.33,0.17,0,0,0,0,0,0,0.00,10.69,0.18,0,0,0,0
> 
> >>> I'd like to be able to populate a vector 'v' s.t.
> >>> v[1]=5718, ... v[43]=0
> 
> Duncan Murdoch Thu, 29 Dec 2011 16:45:45 -0500
> >> x <- read.csv("foo.csv", nrow=1)
> >> x <- as.numeric(x[1,])  # convert to numeric vector
> 
> Aha!
> 
> William Dunlap Thu, 29 Dec 2011 21:49:13 +0000
> > Look into connection objects, which let you open a file or other
> > readable sort of thing and read it piece by piece.
> 
> Will do, since what I plan to use R for (mostly) is manipulating very
> large netCDF files.
> 
> thanks, all! Tom Roche <Tom_Roche at pobox.com>
> 

Tom,

you might also want to see what is already available in R for handling netCDF files.  Install the sos package (if you haven't already) and run the command

findFn('netCDF') 

It returned quite a lot of functions for dealing with netCDF  files/data.


Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA



More information about the R-help mailing list