[R] reading row vectors from file

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Thu Mar 3 23:17:19 CET 2005


Why not simply read it as an csv file, then transpose it. If you also
store it as a data frame, you can use attach() or detach() the object to
the search path whenever you want to access the variables directly.


df <- read.csv( "tmp.txt", header=FALSE, row.names=1 )
df <- data.frame( t( df ) )
df
    freq noise signal pctrcv
V2     0    49     99      5
V3     1    47      0      0
V4     2    48    100      5
V5     3    48      0      0
V6     4    50      0      0
V7     5    47    100      5
V8     6    48      0      0
V9     7    47    100      5
V10    8    46    100     11
V11    9    50      0      0
V12   16    48    100      5
V13   17    54    101      5
V14   18    49    100      5
V15   19    47      0      0
V16   20    49      0      0


attach( df )
pctrcv
 [1]  5  0  5  0  0  5  0  5 11  0  5  5  5  0  0

signal / noise
 [1] 2.020408 0.000000 2.083333 0.000000 0.000000 2.127660 0.000000
2.127660
 [9] 2.173913 0.000000 2.083333 1.870370 2.040816 0.000000 0.000000



On Thu, 2005-03-03 at 12:22 -0900, Ken Irving wrote:
> 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?
> 
> Thanks for any help or leads,
>   
> Ken
>




More information about the R-help mailing list