I have many vectors in the form:
structure(list(v1 = 1:6, V2 = 7:12, V3 = 13:18, v4 = 19:24, v5 = 25:30,
v6 = 31:36), class = "data.frame", row.names = c(NA, -6L))
and want to average every n = 2 consecutive values in each column. The code:
ds<-data.frame(do.call(cbind, lapply(seq(1, ncol(td), by = 2), function(idx) rowMeans(td[c(idx, idx + 1)]))))
gives me:
structure(list(X1 = c(3.5, 9.5), X2 = c(15.5, 21.5), X3 = c(27.5,
33.5)), class = "data.frame", row.names = c("v1", "V2"))
but actually I would expect 6 vectors with 3 rows each as
df <-data.frame(v1 = c(1.5,3.5,5.5),v2 = c(7.5,9.5,11.5),v3=c(13.5,15.5,17.5),v4=c(19.5,21.5,23.5),v5=c(25.5,27.5,29.5),v6=c(31.5,33.5,35.5))
Thank you for any help.