I have a data frame with 2 columns and 3659 row df
I am trying to reduce the data set by averaging every 10 or 13 rows in this data frame, so I tried the following :
# number of rows per group
n=13
# number of groups
n_grp=nrow(df)/n
round(n_grp,0)
# row indices (one vector per group)
idx_grp <- split(seq(df), rep(seq(n_grp), each = n))
# calculate the col means for all groups
res <- lapply(idx_grp, function(i) {
# subset of the data frame
tmp <- dat[i]
# calculate row means
colMeans(tmp, na.rm = TRUE)
})
# transform list into a data frame
dat2 <- as.data.frame(res)
However, I can't divide my number of rows by 10 or 13 because data length is not a multiple of split variable. So I am not sure what should do then (I just want may be to calculate the mean of the last group -even with less than 10 elements)
I also tried this one, but the results are the same:
df1=split(df, sample(rep(1:301, 10)))