R-Helpers: There are a matrix x and a factor f. nrow(x) == length(f), e.g.: x <- matrix(1:6, nrow = 3) f <- factor(c("daytime", "daytime", "night")) I want the sum of all elements of rows of "x" for each corresponding level in factor "f", In this case, I want output like: "daytime" [1] x[1,1]+x[2,1]+x[1,2]+x[2,2] "night" [2] x[3,1]+x[3,2] But, tapply(x,f,sum) or by(x,f,sum) do not work. What other functions can I use? Thank you very much Xiao
> sapply(split(x,f), sum)daytime night 12 9 HTH, Andy> From: XIAO LIU > > R-Helpers: > > There are a matrix x and a factor f. nrow(x) == length(f), e.g.: > x <- matrix(1:6, nrow = 3) > f <- factor(c("daytime", "daytime", "night")) > > I want the sum of all elements of rows of "x" for each > corresponding level in factor "f", > In this case, I want output like: > "daytime" [1] x[1,1]+x[2,1]+x[1,2]+x[2,2] > "night" [2] x[3,1]+x[3,2] > > But, tapply(x,f,sum) or by(x,f,sum) do not work. What other > functions can I use? > > Thank you very much > > Xiao------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}
Gabor Grothendieck
2004-Feb-18 00:10 UTC
[R] Apply a function to each cell of a ragged matrix
rowsum(x,f) --- Date: Tue, 17 Feb 2004 17:38:46 -0500 From: XIAO LIU <xiaoliu at jhmi.edu> To: R Help <r-help at stat.math.ethz.ch> Subject: [R] Apply a function to each cell of a ragged matrix R-Helpers: There are a matrix x and a factor f. nrow(x) == length(f), e.g.: x <- matrix(1:6, nrow = 3) f <- factor(c("daytime", "daytime", "night")) I want the sum of all elements of rows of "x" for each corresponding level in factor "f", In this case, I want output like: "daytime" [1] x[1,1]+x[2,1]+x[1,2]+x[2,2] "night" [2] x[3,1]+x[3,2] But, tapply(x,f,sum) or by(x,f,sum) do not work. What other functions can I use? Thank you very much Xiao
Mahmoud K. Okasha
2004-Feb-18 00:46 UTC
[R] Apply a function to each cell of a ragged matrix
Hi, you could simply use functions such as: time <- dim (3) for ( i in 1:3) time [i] <- x[i,1]+x[i,2] the result of time will be the sum of rows. best regards.. ----- Original Message ----- From: "XIAO LIU" <xiaoliu at jhmi.edu> To: "R Help" <r-help at stat.math.ethz.ch> Sent: Wednesday, February 18, 2004 12:38 AM Subject: [R] Apply a function to each cell of a ragged matrix> R-Helpers: > > There are a matrix x and a factor f. nrow(x) == length(f), e.g.: > x <- matrix(1:6, nrow = 3) > f <- factor(c("daytime", "daytime", "night")) > > I want the sum of all elements of rows of "x" for each corresponding levelin factor "f",> In this case, I want output like: > "daytime" [1] x[1,1]+x[2,1]+x[1,2]+x[2,2] > "night" [2] x[3,1]+x[3,2] > > But, tapply(x,f,sum) or by(x,f,sum) do not work. What other functions canI use?> > Thank you very much > > Xiao > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html> >
Gabor Grothendieck
2004-Feb-18 01:44 UTC
[R] Apply a function to each cell of a ragged matrix
I misread your post. Try any of these: rowSums(rowsum(x,f)) rowsum(rowSums(x),f) tapply(rowSums(x),f,sum) by(rowSums(x),f,sum) --- Date: Tue, 17 Feb 2004 19:10:11 -0500 (EST) From: Gabor Grothendieck <ggrothendieck at myway.com> To: <xiaoliu at jhmi.edu>, <r-help at stat.math.ethz.ch> Subject: RE: [R] Apply a function to each cell of a ragged matrix rowsum(x,f) --- Date: Tue, 17 Feb 2004 17:38:46 -0500 From: XIAO LIU <xiaoliu at jhmi.edu> To: R Help <r-help at stat.math.ethz.ch> Subject: [R] Apply a function to each cell of a ragged matrix R-Helpers: There are a matrix x and a factor f. nrow(x) == length(f), e.g.: x <- matrix(1:6, nrow = 3) f <- factor(c("daytime", "daytime", "night")) I want the sum of all elements of rows of "x" for each corresponding level in factor "f", In this case, I want output like: "daytime" [1] x[1,1]+x[2,1]+x[1,2]+x[2,2] "night" [2] x[3,1]+x[3,2] But, tapply(x,f,sum) or by(x,f,sum) do not work. What other functions can I use?