[Solved] Find the mean for every selected rows in R from csv file [closed]


We can use lapply to loop over the columns of dataset, then created a grouping variable with %/% and get the mean using tapply

lapply(df1, function(x) tapply(x, (seq_along(x)-1)%/%128, FUN = mean, na.rm = TRUE))

data

set.seed(24)
df1 <- data.frame(col1 = sample(c(NA, 1:10), 140, replace=TRUE),
      col2 = sample(c(NA, 1:3), 140, replace=TRUE))

8

solved Find the mean for every selected rows in R from csv file [closed]