Dear Contributors, I am asking help on the way how to solve a problem related to loops for that I always get confused with. I would like to perform the following procedure in a compact way. Consider that p is a matrix composed of 100 rows and three columns. I need to calculate the sum over some rows of each column separately, as follows: fa1<-(colSums(p[1:25,])) fa2<-(colSums(p[26:50,])) fa3<-(colSums(p[51:75,])) fa4<-(colSums(p[76:100,])) fa5<-(colSums(p[1:100,])) and then I need to apply to each of them the following: fa1b<-c() for (i in 1:3){ fa1b[i]<-(100-(100*abs(fa1[i]/sum(fa1[i])-(1/3)))) } fa2b<-c() for (i in 1:3){ fa2b[i]<-(100-(100*abs(fa2[i]/sum(fa2[i])-(1/3)))) } and so on. Is there a more efficient way to do this? Thanks for your time! Francesca ---------------------------------- Francesca Pancotto, PhD Università di Modena e Reggio Emilia Viale A. Allegri, 9 40121 Reggio Emilia Office: +39 0522 523264 Web: https://sites.google.com/site/francescapancotto/ ---------------------------------- [[alternative HTML version deleted]]
Hello, I think there is an error in the expression 100-(100*abs(fa1[i]/sum(fa1[i])-(1/3))) Note that fa1[i]/sum(fa1[i]) is always 1. If it's fa1[i]/sum(fa1), try the following, using lists to hold the results. # Make up some data set.seed(6628) p <- matrix(runif(300), nrow = 100) idx <- seq(1, 100, by = 25) fa <- lapply(idx, function(i) colSums(p[i:(i + 24), ])) fa[[5]] <- colSums(p) fab <- lapply(fa, function(x) 100 - 100*abs(x/sum(x) - 1/3)) fab You can give names to the lists elements, if you want to. names(fa) <- paste0("fa", 1:5) names(fab) <- paste0("fa", 1:5, "b") Hope this helps, Rui Barradas Em 27-01-2013 08:02, Francesca escreveu:> Dear Contributors, > I am asking help on the way how to solve a problem related to loops for > that I always get confused with. > I would like to perform the following procedure in a compact way. > > Consider that p is a matrix composed of 100 rows and three columns. I need > to calculate the sum over some rows of each > column separately, as follows: > > fa1<-(colSums(p[1:25,])) > > fa2<-(colSums(p[26:50,])) > > fa3<-(colSums(p[51:75,])) > > fa4<-(colSums(p[76:100,])) > > fa5<-(colSums(p[1:100,])) > > > > and then I need to apply to each of them the following: > > > fa1b<-c() > > for (i in 1:3){ > > fa1b[i]<-(100-(100*abs(fa1[i]/sum(fa1[i])-(1/3)))) > > } > > > fa2b<-c() > > for (i in 1:3){ > > fa2b[i]<-(100-(100*abs(fa2[i]/sum(fa2[i])-(1/3)))) > > } > > > and so on. > > Is there a more efficient way to do this? > > Thanks for your time! > > Francesca > > ---------------------------------- > Francesca Pancotto, PhD > Universit? di Modena e Reggio Emilia > Viale A. Allegri, 9 > 40121 Reggio Emilia > Office: +39 0522 523264 > Web: https://sites.google.com/site/francescapancotto/ > ---------------------------------- > > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
> Dear Contributors, > I am asking help on the way how to solve a problem related to loops for > that I always get confused with. > I would like to perform the following procedure in a compact way. > > Consider that p is a matrix composed of 100 rows and three columns. I need > to calculate the sum over some rows of each > column separately, as follows: > > fa1<-(colSums(p[1:25,])) > > fa2<-(colSums(p[26:50,])) > > fa3<-(colSums(p[51:75,])) > > fa4<-(colSums(p[76:100,])) > > fa5<-(colSums(p[1:100,])) > > > > and then I need to apply to each of them the following: > > > fa1b<-c() > > for (i in 1:3){ > > fa1b[i]<-(100-(100*abs(fa1[i]/sum(fa1[i])-(1/3)))) > > } >I think I'd do it this way (correcting for the presumed error that Rui Barradas noted):> dim( p ) = c(25,4,3)> p2 = apply(p, c(2,3), sum) > p3 = t(apply(p2, 1, function(fa) 100-(100*abs(fa/sum(fa)-(1/3))) ) )p3 now contains all your results except the one including all the data, which is trivial to compute. -- Richard D. Morey Assistant Professor Psychometrics and Statistics Rijksuniversiteit Groningen / University of Groningen http://drsmorey.org/research/rdmorey