Here is a self-contained example of what you might be trying to do. You would get better answers if you supplied this yourself.
dataset1 <- data.frame(Date=as.POSIXct(c("2015-04-01","2015-04-01","2015-04-07", "2015-04-19")), Weight=11:14) datums <- as.POSIXct(c("2015-04-01", "2015-04-08", "2015-04-19")) # note that neither dataset1$Date nor datums is a subset of the other for (i in 1:length(datums)){ meanweight <- mean(dataset1[dataset1$Date==datums[i],]$Weight) } You said is 'obviously' doesn't work, but didn't say in what was wrong with what it did. One obvious (to me) thing is that in each iteration you overwrote the meanweight assigned in the previous iteration so you end up with only the last one calculated. You can fix that by using making meanweight a vector and assigning elements of it in the loop. meanWeight <- numeric(length(datums)) for (i in 1:length(datums)){ meanWeight[i] <- mean(dataset1[dataset1$Date==datums[i],]$Weight) } data.frame(datums, meanWeight) # datums meanWeight #1 2015-04-01 11.5 #2 2015-04-08 NaN #3 2015-04-19 14.0 This looks to me like the appropriate result, but your self-contained example would make me more convinced of that. By the way, why is is important to you to use a for loop and not a function like tapply() or aggregate()? They can cause hassles with the data I gave above because datums does not partition dataset1$Date, but I don't know what your problem with them is. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, May 20, 2015 at 3:03 AM, Dieter Anseeuw <dieter.anse...@inagro.be> wrote: > Dear all, > I would like to do multiple actions on a subset of my data. Therefore, I > want to create a for loop on the variable "Date" (actually a double for > loop on yet another variable, but let's omit that for a moment). > I want to run down every level of "Date" and perform multiple actions on > the data from a certain date. Here is my code: > > for (i in 1:length(datums)){ > meanweight<-mean(dataset1[dataset1$Date==datums[i],]$Weight) > ... > > However, this subsetting obviously doesn't work. How can I adjust my code > so that R runs down all levels of Data in a for loop? > (I need the for loop, not tapply(), sapply(), ...) > > Thanks in advance, > Dieter Anseeuw > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.