Thank you very much for the help. I decided to go with the 'plyr' package but
it is nice to have options.
Have a lovely day.
--
View this message in context:
http://r.789695.n4.nabble.com/Aggregate-certain-rows-in-a-matrix-tp2528454p2529238.html
Sent from the R help mailing list archive at Nabb
On Sep 6, 2010, at 10:47 AM, Dimitris Rizopoulos wrote:
one way is the following:
M <- cbind(c(1,1,1,1,2,2,3,3,3,3), c(2,2,2,3,4,4,4,5,5,6),
c(1,2,3,4,5,6,7,8,9,10))
ind <- do.call(paste, c(as.data.frame(M[, 1:2], sep = "\r")))
M[, 3] <- ave(M[, 3], ind, FUN = "sum")
unique(M)
I had been
On Mon, Sep 6, 2010 at 3:29 PM, Kennedy wrote:
> I want to reduce the matrix according to the following: If the values of the
> two first columns are the same in two or more rows the values in the third
> column of the corresponding rows should be added and only one of the rows
> should be keept.
one way is the following:
M <- cbind(c(1,1,1,1,2,2,3,3,3,3), c(2,2,2,3,4,4,4,5,5,6),
c(1,2,3,4,5,6,7,8,9,10))
ind <- do.call(paste, c(as.data.frame(M[, 1:2], sep = "\r")))
M[, 3] <- ave(M[, 3], ind, FUN = "sum")
unique(M)
I hope it helps.
Best,
Dimitris
On 9/6/2010 4:29 PM, Kennedy wrot
Hi,
I have a matrix that looks like this
a <- c(1,1,1,1,2,2,3,3,3,3)
b <- c(2,2,2,3,4,4,4,5,5,6)
c <- c(1,2,3,4,5,6,7,8,9,10)
M <- matrix(nr=10,nc=3)
M[,1] <- a
M[,2] <- b
M[,3] <- c
> M
[,1] [,2] [,3]
[1,]121
[2,]122
[3,]123
[4,]
5 matches
Mail list logo