I have a 5-row matrix called “data”. There are headers. it look like this : Row 1 Row2 Row3 Row4 Row5 Line1 … … … … Line2 … … … … Line3 … … … … … Line 1838 … … … … I want to calculate several simple arithmetic means for Row5, that is from Line173 to Line193, from Line434 to Line455, from Line699 to Line724, from Line955 to Line977 A simple way is to individually compute these means. The first one (Line173 to Line193) is then obtained by :>mean(data[173:193,5], na.rm = TRUE)That’s right ! But, I aim to automate this calculation by creating a loop. I tried many things, but I have been unsuccessful at using the ‘for’ loop function. Please, don’t explain things too hard as it is often the case that people wants to help but gets into too much sophistication. Someone can write that little code for this example ? Or suggest functions ? Thanks for your help. Cheers, Édouard. [[alternative(swapped) HTML version deleted]] This is MIME Epilogue
Hi everyone. And, Merry Xmas ! I have a 5-row matrix called “data”. There are headers. it look like this : Row 1 Row2 Row3 Row4 Row5 Line1 … … … … Line2 … … … … Line3 … … … … … Line 1838 … … … … I want to calculate several simple arithmetic means for Row5, that is from Line173 to Line193, from Line434 to Line455, from Line699 to Line724, from Line955 to Line977 A simple way is to individually compute these means. The first one (Line173 to Line193) is then obtained by :>mean(data[173:193,5], na.rm = TRUE)That’s right ! But, I aim to automate this calculation by creating a loop. I tried many things, but I have been unsuccessful at using the ‘for’ loop function. Please, don’t explain things too hard as it is often the case that people wants to help but gets into too much sophistication. Someone can write that little code for this example ? Or suggest functions ? Thanks for your help. Cheers, Édouard. [[alternative(swapped) HTML version deleted]] This is MIME Epilogue
On Dec 27, 2009, at 9:15 AM, Edouard Tallent wrote:> Hi everyone. And, Merry Xmas ! > I have a 5-row matrix called ?data?. There are headers. > it look like this : > Row 1 Row2 Row3 Row4 Row5 > Line1 ? ? ? ? > Line2 ? ? ? ? > Line3 ? ? ? ? > ? > Line 1838 ? ? ? ? > I want to calculate several simple arithmetic means for Row5, that > is from Line173 to Line193, from Line434 to Line455, from Line699 to > Line724, from Line955 to Line977 > A simple way is to individually compute these means. The first one > (Line173 to Line193) is then obtained by : >> mean(data[173:193,5], na.rm = TRUE) > That?s right ! But, I aim to automate this calculation by creating a > loop. > I tried many things, but I have been unsuccessful at using the ?for? > loop function.Generally it would be better to include the failed efforts in the form of executable R code that would let the readership see, for instance, the list (or vector, or matrix) of intervals that you were trying to use in the for loops.> Please, don?t explain things too hard as it is often the case that > people wants to help but gets into too much sophistication. > Someone can write that little code for this example ?? No executable example offered.> Or suggest functions ?A for loop would seem sensible. The sapply function might be used, or perhaps the apply function. It really depends on the details of how the intervals are represented (which you have not provided.)> > and provide commented, minimal, self-contained, reproducible code.^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ David Winsemius, MD Heritage Laboratories West Hartford, CT
An alternative approach (clumsy but probably not as clumsy as a loop) would be to create a sixth column as a factor and aggregate on that. Simple-minded example : ======================================================mydata <- data.frame(matrix(rnorm(100),nrow=20)) mydata[,6] <- c(rep("a",5),rep("b",20-10),rep("c",5)) aggregate(mydata[,5], by=list(mydata[,6]), mean) =========================================================== Set all the unwanted values to X and subset on that before calculating. --- On Sun, 12/27/09, Edouard Tallent <tallent_e at lycos.com> wrote:> From: Edouard Tallent <tallent_e at lycos.com> > Subject: [R] how to create a simple loop ? > To: r-help at r-project.org > Received: Sunday, December 27, 2009, 9:15 AM > Hi everyone. And, Merry Xmas ! > I have a 5-row matrix called ?data?. There are > headers. > it look like this : > Row 1???Row2? ? Row3? ? > Row4? ? Row5 > Line1? ????? ? ? > ? ? ? ?? ? ? ? ? > ? ?? ? ? ? ? ? ? > Line2? ????? ? ? > ? ? ? ?? ? ? ? ? > ? ?? ? ? ? ? ? ? > Line3? ????? ? ? > ? ? ? ?? ? ? ? ? > ? ?? ? ? ? ? ? ? > ? > Line 1838? ? ? ? ? > ????? ? ? ? ? > ? ?? ? ? ? ? ? > ?? ? ? ? ? ? ? > I want to calculate several simple arithmetic means for > Row5, that is from Line173 to Line193, from Line434 to > Line455, from Line699 to Line724, from Line955 to Line977 > A simple way is to individually compute these means. The > first one (Line173 to Line193) is then obtained by : > >mean(data[173:193,5], na.rm = TRUE) > That?s right ! But, I aim to automate this calculation by > creating a loop. > I tried many things, but I have been unsuccessful at using > the ?for? loop function. > Please, don?t explain things too hard as it is often the > case that people wants to help but gets into too much > sophistication. > Someone can write that little code for this example ? Or > suggest functions ? > Thanks for your help. > Cheers, > ?douard. > > ??? [[alternative(swapped) HTML version > deleted]] > > This is MIME Epilogue > > > -----Inline Attachment Follows----- > > ______________________________________________ > 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. >__________________________________________________________________ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/
try this:> mydata <- matrix(runif(5000),ncol=5) > indices <- list(c(173, 193),+ c(434, 455), + c(699, 724), + c(955, 977))> result <- lapply(indices, function(.indx){+ mean(mydata[.indx[1]:.indx[2], 5]) + })> > > result[[1]] [1] 0.5364535 [[2]] [1] 0.4080824 [[3]] [1] 0.5109079 [[4]] [1] 0.4392921>On Sun, Dec 27, 2009 at 9:14 AM, Edouard Tallent <tallent_e@lycos.com>wrote:> I have a 5-row matrix called “data”. There are headers. > it look like this : > Row 1 Row2 Row3 Row4 Row5 > Line1 … … … … > Line2 … … … … > Line3 … … … … > … > Line 1838 … … … … > I want to calculate several simple arithmetic means for Row5, that is from > Line173 to Line193, from Line434 to Line455, from Line699 to Line724, from > Line955 to Line977 > A simple way is to individually compute these means. The first one > (Line173 to Line193) is then obtained by : > >mean(data[173:193,5], na.rm = TRUE) > That’s right ! But, I aim to automate this calculation by creating a loop. > I tried many things, but I have been unsuccessful at using the ‘for’ loop > function. > Please, don’t explain things too hard as it is often the case that people > wants to help but gets into too much sophistication. > Someone can write that little code for this example ? Or suggest functions > ? > Thanks for your help. > Cheers, > Édouard. > > [[alternative(swapped) HTML version deleted]] > > This is MIME Epilogue > > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]