Re: [R] reading row vectors from file

From: Gabor Grothendieck <ggrothendieck_at_myway.com>
Date: Fri 04 Mar 2005 - 09:16:56 EST

Gabor Grothendieck <ggrothendieck <at> myway.com> writes:

:
: Ken Irving <fnkci <at> uaf.edu> writes:
:
: :
: : Hi,
: :
: : New to R, using version 2.0.1 (2004-11-15) on debian Linux (sid), kernel
: : 2.6.8-2-686.
: :
: : I have data in files with separate vectors on each row of the file,
: : e.g.,
: :
: : $ cat /tmp/stats
: : freq,0,1,2,3,4,5,6,7,8,9,16,17,18,19,20,...
: : noise,49,47,48,48,50,47,48,47,46,50,48,54,49,47,49,...
: : signal,99,0,100,0,0,100,0,100,100,0,100,101,100,0,0,...
: : pctrcv,5,0,5,0,0,5,0,5,11,0,5,5,5,0,0,...
: :
: : I can transpose the data file (e.g., using an awk script), and then read
: : it using read.csv("tstats"),
: :
: : $ transpose /tmp/stats > /tmp/tstats
: : $ cat /tmp/tstats
: : freq,noise,signal,pctrcv
: : 0,49,99,5
: : 1,47,0,0
: : 2,48,100,5
: : ...
: :
: : but would prefer to import the line-oriented files directly. I've
: : drawn a blank after perusing help, documentation, google searches, etc..
: :
: : Something like read.csv( "transpose stat |" ) might be nice, e.g., the
: : trailing pipe symbol invokes the argument as a shell pipeline, then
: : reads from stdin, but I'm just making this up... Actually, this does
: : work:
: :
: : t <- read.csv( pipe("transpose stat1") )
: :
: : but it does rely on an external transpose command. Is there a way to
: : read line-oriented vector files directly?
: :
:
: for(v in strsplit(readLines(myfile), ",")) assign(v[[1]], as.numeric(v[-1]))
:

One more comment. If the vectors are of the same length and you want to create a data frame out of them you could do this:

e <- new.env()
# next line is same as previous solution except for e in 3rd arg of assign for(v in strsplit(readLines(myfile), ",")) assign(v[[1]], as.numeric(v[-1]),e) as.data.frame(as.list(e))



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 Received on Fri Mar 04 09:49:10 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:40 EST