Re: [R] Sum every n (4) observations by group

2021-12-20 Thread Rasmus Liland
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, ife

Re: [R] Sum every n (4) observations by group

2021-12-20 Thread Leonard Mada via R-help
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

Re: [R] Sum every n (4) observations by group

2021-12-19 Thread Avi Gross via R-help
-Original Message- From: R-help On Behalf Of Miluji Sb Sent: Sunday, December 19, 2021 1:32 PM To: r-help mailing list Subject: [R] Sum every n (4) observations by group Dear all, I have a dataset (below) by ID and time sequence. I would like to sum every four observations by ID. I am

Re: [R] Sum every n (4) observations by group

2021-12-19 Thread Miluji Sb
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 =

Re: [R] Sum every n (4) observations by group

2021-12-19 Thread Peter Langfelder
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,

[R] Sum every n (4) observations by group

2021-12-19 Thread Miluji Sb
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