将变量分布在dplyr中的多个列中

时间:2019-04-10 14:33:13

标签: r dplyr

假设我有以下数据集:

df <- read.table(header=TRUE, text="
politics_collapse question_id mean_confidence mean_accuracy mean_importance
Democrat arms_manufacturing_company 24.00000 0.0000000 1.000000
Democrat black_panther 48.50000 0.0000000 1.500000
Democrat stranger_things_universe 55.50000 0.2500000 2.500000
Democrat the_office 37.66667 0.6666667 1.666667
Democrat tupac 80.33333 1.0000000 2.000000
Democrat uber_ceo 39.60000 0.8000000 2.600000
Republican arms_manufacturing_company 37.00000 1.0000000 1.000000
Republican black_panther 45.00000 1.0000000 2.000000
Republican stranger_things_universe 33.00000 1.0000000 3.000000")

我正在尝试将politics_collapse列分散到mean_confidence, mean_accuracy, and mean_importance列中。结果输出将是mean_confidence_democratmean_accuracy_democratmean_importance_democrat ...,共和党也一样。

像这样:

df <- read.table(header=TRUE, text="
question_id mean_confidence_democrat mean_accuracy_democrat mean_importance_democrat mean_confidence_republican mean_accuracy_republican mean_importance_republican
arms_manufacturing_company 
black_panther 
stranger_things_universe 
the_office 
tupac 
uber_ceo 
arms_manufacturing_company 
black_panther 
stranger_things_universe")

显然,每行中都有数字值。

我在这里遇到了这个小插图:https://community.rstudio.com/t/spread-with-multiple-value-columns/5378,它建议使用全新的“枢轴功能”,但我不知道如何使它们起作用。我还尝试了嵌套值,传播它们和取消嵌套,但没有使它起作用。

2 个答案:

答案 0 :(得分:2)

这可能是您想要的:

library(tidyverse)

df %>%
  gather("metric", "score", mean_confidence, mean_accuracy, mean_importance) %>%
  mutate(metric = paste0(metric, "_", politics_collapse)) %>%
  select(-politics_collapse) %>%
  spread(metric, score)

                 question_id mean_accuracy_Democrat mean_accuracy_Republican mean_confidence_Democrat mean_confidence_Republican mean_importance_Democrat
1 arms_manufacturing_company              0.0000000                        1                 24.00000                         37                 1.000000
2              black_panther              0.0000000                        1                 48.50000                         45                 1.500000
3   stranger_things_universe              0.2500000                        1                 55.50000                         33                 2.500000
4                 the_office              0.6666667                       NA                 37.66667                         NA                 1.666667
5                      tupac              1.0000000                       NA                 80.33333                         NA                 2.000000
6                   uber_ceo              0.8000000                       NA                 39.60000                         NA                 2.600000
  mean_importance_Republican
1                          1
2                          2
3                          3
4                         NA
5                         NA
6                         NA

答案 1 :(得分:1)

在新的R函数正式发布之前,这是使用pivot执行此操作的简单方法:

tidyr

给予:

df %>% 
    tidyr::gather(variable, value, mean_confidence, mean_accuracy, mean_importance) %>%
    tidyr::unite(new_columns, politics_collapse, variable) %>%
    tidyr::spread(new_columns, value)