Mutate multiple / consecutive columns (with dplyr or base R)

前端 未结 3 1883
独厮守ぢ
独厮守ぢ 2020-12-06 11:33

I\'m trying to create \"waves\" of variables that represent repeated measures. Specifically, I\'m trying to create consecutive variables that represent the mean values for

3条回答
  •  失恋的感觉
    2020-12-06 12:15

    Here is one way with the package zoo:

    library(zoo)
    t(rollapply(t(df), width = 10, by = 10, function(x) sum(x)/10))
    

    Here is one way to do it with base R:

    splits <- 1:100
    dim(splits) <- c(10, 10)
    splits <- split(splits, col(splits))
    results <- do.call("cbind", lapply(splits, function(x) data.frame(rowSums(df[,x] / 10))))
    names(results) <- paste0("wave_", 1:10)
    results
    

    Another very succinct way with base R (courtesy of G.Grothendieck):

    t(apply(df, 1, tapply, gl(10, 10), mean))
    

    And here is a solution with dplyr and tidyr:

    library(dplyr)
    library(tidyr)
    df$row <- 1:nrow(df)
    df2 <- df %>% gather(column, value, -row)
    df2$column <- cut(as.numeric(gsub("X", "", df2$column)),breaks = c(0:10*10))
    df2 <- df2 %>% group_by(row, column) %>% summarise(value = sum(value)/10)
    df2 %>% spread(column, value) %>% select(-row)
    

提交回复
热议问题