1

我有一个来自我的大数据集的分组数据框,它有大约 800 列和大约 250 万条记录。我正在尝试创建一行,每列仅包含 5-10 列,但不知道为什么,我一直在获取NA所有行的均值。

这是我尝试过的:

clean_bmk <- clean_bmk %>% 
                rowwise() %>%
                mutate(
                       BMK_Mean_Strategic = mean(!!strategic, na.rm = T),
                       BMK_Mean_DiffChange = mean(!!diffchange, na.rm = T),
                       BMK_Mean_Failure = mean(!!failure, na.rm = T),
                       BMK_Mean_Narrow = mean(!!narrow, na.rm = T),
                       BMK_R1_Performance = mean(!!performance_vars, na.rm=T),
                       BMK_R2_Promotion = mean(!!promote_vars, na.rm=T),
                       BMK_R3_Derail = mean(!!derail_vars, na.rm=T))


class(clean_bmk)
[1] "grouped_df" "tbl_df"     "tbl"        "data.frame"

当我这样做时,所有变异的列都是 NA。但是,以下工作:

clean_bmk$Strategic_Mean <- rowMeans(clean_bmk[,strategic], na.rm=T)

不知道为什么,以及如何制作一个函数,以便我只能发送包含列名的变量列表,并改变数据框中的列?

例如:

strategic <- c("column1", "column15", "column27")

与其他变量类似diffchange,如failure, 等。

我试图dput(clean_bmk)与您分享数据,但由于数据集很大,我无法获得它。我猜是因为它是一个grouped_df,我不能使用[[也不能使用sample()数据集。

4

1 回答 1

1

使用它会效率低下rowwise,而更好的选择是rowMeansselect感兴趣的列之后

library(dplyr)
clean_bmk %>% 
    ungroup %>%
    mutate(
      BMK_Mean_Strategic = rowMeans(select(., strategic),  na.rm = TRUE),
       BMK_Mean_DiffChange = rowMeans(select(., diffchange), na.rm = TRUE),
       BMK_Mean_Failure = rowMeans(select(., failure), na.rm = TRUE),
       BMK_Mean_Narrow = rowMeans(select(., narrow), na.rm = TRUE),
       BMK_R1_Performance = rowMeans(select(., performance_vars), na.rm=TRUE),
       BMK_R2_Promotion = rowMeans(select(., promote_vars), na.rm=TRUE),
       BMK_R3_Derail = rowMeans(select(., derail_vars), na.rm=TRUE))

使用可重现的示例

data(mtcars)
#v1 <- c('mpg', 'disp')
mtcars %>%
   transmute(newMean = rowMeans(select(., v1), na.rm = TRUE)) %>%
   head  
#                  newMean
#Mazda RX4           90.50
#Mazda RX4 Wag       90.50
#Datsun 710          65.40
#Hornet 4 Drive     139.70
#Hornet Sportabout  189.35
#Valiant            121.55
于 2020-02-28T18:14:18.600 回答