基于dplyr的R中其他列的条件均值

时间:2019-04-09 23:48:15

标签: r dplyr

假设我有以下数据:

structure(list(political_spectrum = c(5L, 15L, 12L, 30L, 100L, 
0L, 27L, 52L, 38L, 64L, 0L, 0L, 76L, 50L, 16L, 16L, 0L, 23L, 
0L, 25L, 68L, 50L, 4L, 0L, 50L), politics_today = c("Independent", 
"Strong Democrat", "Weak Democrat", "Weak Democrat", "Weak Republican", 
"Strong Democrat", "Weak Democrat", "Weak Democrat", "Independent", 
"Weak Democrat", "Strong Democrat", "Independent", "Weak Republican", 
"Weak Democrat", "Weak Democrat", "Strong Democrat", "Strong Democrat", 
"Strong Democrat", "Strong Democrat", "Strong Democrat", "Independent", 
"Independent", "Strong Democrat", "Strong Democrat", "Independent"
), stranger_things_universe_mc = c("The Demagorgon", "", "", 
"", "", "", "", "", "", "The Stranger Land", "The Demagorgon", 
"The Upside Down", "", "", "", "", "", "The Upside Down", "The Shadowland", 
"", "", "", "", "", "The Shadowland"), stranger_things_universe_answer = c("The Upside Down", 
"", "", "", "", "", "", "", "", "The Upside Down", "The Upside Down", 
"The Upside Down", "", "", "", "", "", "The Upside Down", "The Upside Down", 
"", "", "", "", "", "The Upside Down"), stranger_things_universe_confidence = c(32L, 
NA, NA, NA, NA, NA, NA, NA, NA, 67L, 94L, 89L, NA, NA, NA, NA, 
NA, 51L, 10L, NA, NA, NA, NA, NA, 0L), stranger_things_universe_importance = c("Don't care at all", 
"", "", "", "", "", "", "", "", "Care somewhat strongly", "Care a little", 
"Care somewhat strongly", "", "", "", "", "", "Care somewhat", 
"Don't care at all", "", "", "", "", "", "Don't care at all"), 
    tupac_mc = c("", "Biggie Smalls", "", "", "", "", "", "Biggie Smalls", 
    "Biggie Smalls", "", "", "Biggie Smalls", "", "", "", "", 
    "", "", "Biggie Smalls", "", "", "Ice Cube", "", "", ""), 
    tupac_answer = c("", "Biggie Smalls", "", "", "", "", "", 
    "Biggie Smalls", "Biggie Smalls", "", "", "Biggie Smalls", 
    "", "", "", "", "", "", "Biggie Smalls", "", "", "Biggie Smalls", 
    "", "", ""), tupac_confidence = c(NA, 70L, NA, NA, NA, NA, 
    NA, 71L, 76L, NA, NA, 100L, NA, NA, NA, NA, NA, NA, 100L, 
    NA, NA, 32L, NA, NA, NA), tupac_importance = c("", "Don't care at all", 
    "", "", "", "", "", "Care somewhat", "Don't care at all", 
    "", "", "Care strongly", "", "", "", "", "", "", "Care a little", 
    "", "", "Don't care at all", "", "", ""), uber_ceo_mc = c("John Zimmer", 
    "", "", "", "", "Travis Kalanick", "", "", "", "Travis Kalanick", 
    "", "", "", "", "", "", "", "John Zimmer", "Travis Kalanick", 
    "Travis Kalanick", "", "", "", "", ""), uber_ceo_answer = c("Travis Kalanick", 
    "", "", "", "", "Travis Kalanick", "", "", "", "Travis Kalanick", 
    "", "", "", "", "", "", "", "Travis Kalanick", "Travis Kalanick", 
    "Travis Kalanick", "", "", "", "", ""), uber_ceo_confidence = c(0L, 
    NA, NA, NA, NA, 94L, NA, NA, NA, 69L, NA, NA, NA, NA, NA, 
    NA, NA, 5L, 13L, 17L, NA, NA, NA, NA, NA), uber_ceo_importance = c("Don't care at all", 
    "", "", "", "", "Care strongly", "", "", "", "Care somewhat", 
    "", "", "", "", "", "", "", "Don't care at all", "Don't care at all", 
    "Care somewhat", "", "", "", "", ""), black_panther_mc = c("", 
    "T'Chaka", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "T'Chaka", "", ""), black_panther_answer = c("", 
    "T'Challa", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "T'Challa", "", ""), black_panther_confidence = c(NA, 
    63L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 34L, NA, NA), black_panther_importance = c("", 
    "Don't care at all", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "Care a little", 
    "", ""), the_office_mc = c("The Mindy Project", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "The Office", "", 
    "", "The Mindy Project", "", "", "", "", "The Office", "", 
    ""), the_office_answer = c("The Office", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "The Office", "", "", 
    "The Office", "", "", "", "", "The Office", "", ""), the_office_confidence = c(43L, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, NA, 
    NA, 11L, NA, NA, NA, NA, 100L, NA, NA), the_office_importance = c("Don't care at all", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "Don't care at all", 
    "", "", "Care a little", "", "", "", "", "Care a little", 
    "", ""), arms_manufacturing_company_mc = c("J. Brockton & Sons", 
    "", "", "O.F. Mossberg & Sons", "", "", "", "", "", "", "", 
    "", "J. Brockton & Sons", "", "", "", "", "", "", "", "", 
    "", "", "", "J. Brockton & Sons"), arms_manufacturing_company_answer = c("J. Brockton & Sons", 
    "", "", "J. Brockton & Sons", "", "", "", "", "", "", "", 
    "", "J. Brockton & Sons", "", "", "", "", "", "", "", "", 
    "", "", "", "J. Brockton & Sons"), arms_manufacturing_company_confidence = c(91L, 
    NA, NA, 24L, NA, NA, NA, NA, NA, NA, NA, NA, 37L, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, 100L), arms_manufacturing_company_importance = c("Don't care at all", 
    "", "", "Don't care at all", "", "", "", "", "", "", "", 
    "", "Don't care at all", "", "", "", "", "", "", "", "", 
    "", "", "", "Don't care at all")), class = c("data.table", 
"data.frame"), row.names = c(NA, -25L))

我正在尝试执行以下操作:

test %>%
  gather(name, value, -c('political_spectrum', 'politics_today')) %>%
  filter(value != "") %>%
  mutate(question_id = sub("_[^_]+$", "", name)) %>%
  mutate(confidence = grepl("_confidence", name)) %>%
  group_by(politics_today, question_id) %>%
  summarize(mean_confidence = mean(value[confidence == "TRUE"]))

其中,我获得每个政治联系的mean_confidence值,但仅针对“值”列中的特定行。为了仅在“置信度”列上运行均值,我试图通过mean(value[confidence == "TRUE"])进行过滤,但是不确定这样做的正确方法。

1 个答案:

答案 0 :(得分:1)

我认为您需要将代码更改为

library(tidyverse)

test %>%
  gather(name, value, -c('political_spectrum', 'politics_today')) %>%
  filter(value != "") %>%
  mutate(question_id = sub("_[^_]+$", "", name),
         confidence = grepl("_confidence", name)) %>%
  group_by(politics_today, question_id) %>%
  summarize(mean_confidence = mean(as.numeric(value[confidence])))


#   politics_today  question_id                mean_confidence
#   <chr>           <chr>                                <dbl>
# 1 Independent     arms_manufacturing_company            95.5
# 2 Independent     stranger_things_universe              40.3
# 3 Independent     the_office                            43  
# 4 Independent     tupac                                 69.3
# 5 Independent     uber_ceo                               0  
# 6 Strong Democrat black_panther                         48.5
# 7 Strong Democrat stranger_things_universe              51.7
# 8 Strong Democrat the_office                            55.5
# 9 Strong Democrat tupac                                 85  
#10 Strong Democrat uber_ceo                              32.2
#11 Weak Democrat   arms_manufacturing_company            24  
#12 Weak Democrat   stranger_things_universe              67  
#13 Weak Democrat   the_office                             2  
#14 Weak Democrat   tupac                                 71  
#15 Weak Democrat   uber_ceo                              69  
#16 Weak Republican arms_manufacturing_company            37  

由于您的value列同时具有数字和字符值,因此它将转换为字符列,因此您需要将value的{​​{1}}更改为数字。