Hi, let's say I have this A = matrix(c(1, 2, 4), nrow=1) colnames(A)=c("YOO1", "YOO2", "YOO3") # ie # YOO1 YOO2 YOO3 #[1,] 1 2 4 HELLO <- NULL HELLO$YOO1="BOO" HELLO$YOO2="BOO" HELLO$YOO3="HOO" and I want a matrix that will sum my categorization.. how can I do it efficiently without any loop? #ie BOO HOO #[1,] 3 4 Thanks a lot! -- View this message in context: http://www.nabble.com/Matrix-manipulation-tf3223616.html#a8953806 Sent from the R help mailing list archive at Nabble.com.
Hi, let's say I have this A = matrix(c(1, 2, 4), nrow=1) colnames(A)=c("YOO1", "YOO2", "YOO3") # ie # YOO1 YOO2 YOO3 #[1,] 1 2 4 HELLO <- NULL HELLO$YOO1="BOO" HELLO$YOO2="BOO" HELLO$YOO3="HOO" and I want a matrix that will sum my categorization.. how can I do it efficiently without any loop? #ie BOO HOO #[1,] 3 4 Thanks a lot! -- View this message in context: http://www.nabble.com/Matrix-manipulation-tf3223618.html#a8953808 Sent from the R help mailing list archive at Nabble.com.
> Hi, let's say I have this > > A = matrix(c(1, 2, 4), nrow=1) > colnames(A)=c("YOO1", "YOO2", "YOO3")Why do you need A to be a matrix and not simply a vector?> > # ie > # YOO1 YOO2 YOO3 > #[1,] 1 2 4 > > HELLO <- NULL > HELLO$YOO1="BOO" > HELLO$YOO2="BOO" > HELLO$YOO3="HOO" >Why do you need HELLO to be a list and not simply a character vector?> and I want a matrix that will sum my categorization.. how can I do it > efficiently without any loop? > > #ie BOO HOO > #[1,] 3 4 >Anyway, here is a solution: x <- tapply(A, match(unlist(HELLO), unique(unlist(HELLO))), sum) names(x) <- unique(unlist(HELLO)) x HTH, Giovanni -- Giovanni Petris <GPetris at uark.edu> Associate Professor Department of Mathematical Sciences University of Arkansas - Fayetteville, AR 72701 Ph: (479) 575-6324, 575-8630 (fax) http://definetti.uark.edu/~gpetris/