Dear Miluji, something like this could help: sapply(tapply(x$Value, x$ID, cumsum), ??? function(x) x[seq(4, length(x), by=4)] - c(0, x[head(seq(4, length(x), by=4), -1)])) 1.) Step 1: Compute the cumsum for each ID: tapply(x$Value, x$ID, cumsum) 2.) Step 2: - iterate over the resulting list and select each 4th value; - you can either run a diff on this or subtract directly the (n-4) sum; Note: - you may wish to check if the last value is a multiple of 4; - alternative: you can do a LOCF (last observation carried forward); Hope this code example helps. Sincerely, Leonard
Dear Miluji, I went through different combinations, and find it elegant to sum the values with respect to ID + High/Low Date levels like if this was a list of Dose/Material/Group experiments: x[x$Date %in% c(4143, 4147),"j"] <- tapply(x$Value, paste0(x$ID, ifelse(x$Date>4143, 2, 1)), sum) This way, you escape the ?every 4th observation? criterium, IMHO instead ?using? the dataframe in contrast to creating new information or interpreting the structure of the data frame ... I'm sorry if this only creates confusion and a ?what is he talking about?!? awkwardness by not answering the question in a precise way ... Best, Rasmus