[R] How to scan concatenated(ish) vectors from a file?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Dec 6 23:21:03 CET 2002


David Pearson <david.pearson at mail.nerc-essc.ac.uk> writes:

> Hi,
> 
> I have a text file containing lists of numbers with the
> following structure:
> 
> a1,1   a1,2   a1,3   a1,4   a1,5 ...  a1,10
> hm1,1  hm1,2  hm1,3  hm1,4  hm1,5 ... hm1,10
> vm1,1  vm1,2  vm1,3  vm1,4  vm1,5 ... vm1,10
> hx1,1  hx1,2  hx1,3  hx1,4  hx1,5 ... hx1,10
> vx1,1  vx1,2  vx1,3  vx1,4  vx1,5 ... vx1,10
> a2,1   a2,2   a2,3   a2,4   a2,5 ...  a2,10

>  ....... etc. .........
> 
> [I have pasted in a sample at the bottom of this mail]

> I need to read these data into R, so that the
> a-vectors form a matrix, the hm-vectors form a
> matrix, and so on for vm, hx and vx.

Try:

arr <- array(scan(),c(10,5,2))
a <- arr[,1,]
hm <- arr[,2,]

and so forth. Or:

vlist <- lapply(1:5, function(i)a[,i,])
names(vlist) <- c("a","hm","vm","hx","vx")
vlist$a 
vlist$hm

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list