Dear Peter, Thanks so much for your reply and the code! This is helpful.
What I would like is the data.frame below - sum values for *4140, 4141, 4142, 4143 *and then for *4144, 4145, 4146, 4147 *for IDs A, B, and C. Does that make sense? Thanks again! Best. Milu results <- structure(list(ID = c("A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C"), Date = c(4140L, 4141L, 4142L, 4143L, 4144L, 4145L, 4146L, 4147L, 4140L, 4141L, 4142L, 4143L, 4144L, 4145L, 4146L, 4147L, 4140L, 4141L, 4142L, 4143L, 4144L, 4145L, 4146L, 4147L ), Value = c(0.000207232, 0.000240141, 0.000271414, 0.000258384, 0.00024364, 0.00027148, 0.000280585, 0.000289691, 0.000298797, 0.000307903, 0.000317008, 0.000326114, 0.00033522, 0.000344326, 0.000353431, 0.000362537, 0.000371643, 0.000380749, 0.000389854, 0.00039896, 0.000408066, 0.000417172, 0.000426277, 0.000435383 ), sum = c(NA, NA, NA, 0.000977171, NA, NA, NA, 0.001054089, NA, NA, NA, 0.001213399, NA, NA, NA, 0.001395514, NA, NA, NA, 0.001541206, NA, NA, NA, 0.001686898)), class = "data.frame", row.names = c(NA, -24L)) On Sun, Dec 19, 2021 at 7:50 PM Peter Langfelder <peter.langfel...@gmail.com> wrote: > I'm not sure I understand the task, but if I do, assuming your data > frame is assigned to a variable named df, I would do something like > > sumNs = function(x, n) > { > if (length(x) %%n !=0) stop("Length of 'x' must be a multiple of 'n'.") > n1 = length(x)/n > ind = rep(1:n1, each = n) > tapply(x, ind, sum) > } > sums = tapply(df$Value, df$ID, sumNs, 4) > > Peter > > On Sun, Dec 19, 2021 at 10:32 AM Miluji Sb <miluj...@gmail.com> wrote: > > > > Dear all, > > > > I have a dataset (below) by ID and time sequence. I would like to sum > every > > four observations by ID. > > > > I am confused how to combine the two conditions. Any help will be highly > > appreciated. Thank you! > > > > Best. > > > > Milu > > > > ## Dataset > > structure(list(ID = c("A", "A", "A", "A", "A", "A", "A", "A", > > "B", "B", "B", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", > > "C", "C", "C"), Date = c(4140L, 4141L, 4142L, 4143L, 4144L, 4145L, > > 4146L, 4147L, 4140L, 4141L, 4142L, 4143L, 4144L, 4145L, 4146L, > > 4147L, 4140L, 4141L, 4142L, 4143L, 4144L, 4145L, 4146L, 4147L > > ), Value = c(0.000207232, 0.000240141, 0.000271414, 0.000258384, > > 0.00024364, 0.00027148, 0.000280585, 0.000289691, 0.000298797, > > 0.000307903, 0.000317008, 0.000326114, 0.00033522, 0.000344326, > > 0.000353431, 0.000362537, 0.000371643, 0.000380749, 0.000389854, > > 0.00039896, 0.000408066, 0.000417172, 0.000426277, 0.000435383 > > )), class = "data.frame", row.names = c(NA, -24L)) > > > > [[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.