[R] changing the form of a list
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Mon Apr 8 17:27:22 CEST 2002
"jimi adams" <imij at columbus.rr.com> writes:
> i have a 2 x n matrix that is a paired list of connections, i am working
> with that looks something like:
> 1 2
> 1 3
> 1 5
> 2 1
> 2 3
> 3 1
> 3 2
> 4 5
> 5 1
> 5 4
> for later operations it would be helpful if i could change this into the
> form of:
> 1 2 3 5
> 2 1 3
> 3 1 2
> 4 5
> 5 1 4
Something like this?
> zz <- matrix(scan(),ncol=2,byrow=T)
1: 1 2
3: 1 3
5: 1 5
7: 2 1
9: 2 3
11: 3 1
13: 3 2
15: 4 5
17: 5 1
19: 5 4
21: Read 20 items
> x <- zz[,1]
> y <- zz[,2]
> tapply(y,x,sort)
$"1"
[1] 2 3 5
$"2"
[1] 1 3
$"3"
[1] 1 2
$"4"
[1] 5
$"5"
[1] 1 4
You might want something slightly more complicated like
tapply(y,factor(x,levels=1:5), function(z) sort(unique(x)))
to account for empty groups and duplicated pairs.
--
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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list