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 the initial list is randomly generated and the max number of times that any one number can appear is previoulsy defined by a limiting function (which is variable depending on the numbers involved) is there a clean way to change the form of that list? i could do it with some big nasty if loops, but i was hoping that there might be a quick generic way to do it that i am missing. jimi adams Department of Sociology The Ohio State University 300 Bricker Hall 190 N Oval Mall Columbus, OH 43210-1353 614-688-4261 our mind has a remarkable ability to think of contents as being independent of the act of thinking -georg simmel -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"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 4Something 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, 8 Apr 2002, jimi adams wrote:> 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 > the initial list is randomly generated and the max number of times that any > one number can appear is previoulsy defined by a limiting function (which is > variable depending on the numbers involved) > is there a clean way to change the form of that list?Suppose the matrix is actually a data.frame (if it isn't, use as.data.frame) called df with columns called i and x tapply(df$x, df$i, c)> dfi x 1 1 1 2 2 2 3 3 3 4 3 4 5 4 5 6 2 6 7 1 7 8 3 8 9 4 9 10 3 10> tapply(df$x,df$i,c)$"1" [1] 1 7 $"2" [1] 2 6 $"3" [1] 3 4 8 10 $"4" [1] 5 9 Or by()> by(df, df$i, function(subset) subset$x)df$i: 1 [1] 1 7 ------------------------------------------------------------ df$i: 2 [1] 2 6 ------------------------------------------------------------ df$i: 3 [1] 3 4 8 10 ------------------------------------------------------------ df$i: 4 [1] 5 9 Or you might want to leave the data in its current form and use tapply() or by() to do whatever it is you want to do with it. -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._