my data frame:
data <- structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L,1L,1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), col1 = c(9,
9.05, 7.15, 7.21, 7.34, 8.12, 7.5, 7.84, 7.8, 7.52, 8.84, 6.98,
6.1, 6.89, 6.5, 7.5, 7.8, 5.5, 6.61, 7.65, 7.68,8.0,9.0), col2 = c(11L,
11L, 10L, 1L, 3L, 7L, 11L, 11L, 11L, 11L, 4L, 1L, 1L, 1L, 2L,
2L, 1L, 4L, 8L, 8L, 1L,3L,4L), col3 = c(7L, 11L, 3L, 7L, 11L, 2L, 11L,
5L, 11L, 11L, 5L, 11L, 11L, 2L, 9L, 9L, 3L, 8L, 11L, 11L, 2L,5L,6L),
col4 = c(11L, 11L, 11L, 11L, 6L, 11L, 11L, 11L, 10L, 7L,
11L, 2L, 11L, 3L, 11L, 11L, 6L, 11L, 1L, 11L, 11L,13L,12L), col5 = c(11L,
1L, 2L, 2L, 11L, 11L, 1L, 10L, 2L, 11L, 1L, 3L, 11L, 11L,
8L, 8L, 11L, 11L, 11L, 2L, 9L,4L,5L)), .Names = c("group", "col1",
"col2", "col3", "col4", "col5"), class = "data.frame", row.names = c(NA,
-21L))
function:
comb <- list(c(2, 4), c(3, 5), c(4, 6))
test.fun <- function(dat) {
do.call(rbind, lapply(comb, function(x) {
SUM <- dat[[x[1]]]+dat[[x[2]]]
data.frame(NAME = sprintf('Group %s by Group %s', x[1], x[2]),
SUM)
}))
}
result <- purrr::map_df(split(data, data$group), test.fun, .id = 'Group')
Now this function processes 2 columns of list in the list(c(2, 4), c(3, 5), c(4, 6)). I want it to process any amount for example:
list(c(2, 4, 6), c(3, 5, 6), c(3, 4, 6), c(2, 3), c(3, 5))