在dplyr中根据其他受访者的答案构造变量

时间:2019-11-20 21:52:42

标签: r dplyr

假设我有以下数据集:

dat <- read.table(text="Q_ID TeamID Answer Confidence Blur
1 A Yes 88 0 
1 A Maybe 99 0
1 B No 20 1
1 B Yes 22 1
1 C Definitely 32 2
1 C Yes 55 1
1 D No 33 5
1 D Maybe 33 9", header=T)

我想按TeamID分组,并创建一系列变量,以反映对方在双子星中的答案。我目前这样做的方式是通过手动构建,如下所示:

> dat %>% mutate(partner_answer =  ave(Answer, TeamID, FUN = rev))
  Q_ID TeamID     Answer Confidence Blur partner_answer
1    1      A        Yes         88    0          Maybe
2    1      A      Maybe         99    0            Yes
3    1      B         No         20    1            Yes
4    1      B        Yes         22    1             No
5    1      C Definitely         32    2            Yes
6    1      C        Yes         55    1     Definitely
7    1      D         No         33    5          Maybe
8    1      D      Maybe         33    9             No

但是,在我的实际数据集中,我想创建约100个变量的“伙伴”版本,可以将其与以下正则表达式匹配:

index <- grepl('Answer|Confidence|Blur', names(dat)) 

在dplyr中是否有一种方便的方法可以自动在索引匹配的列上构造所有伙伴变量? (例如partner_confidencepartner_blur

1 个答案:

答案 0 :(得分:1)

这将反转每个对偶的“答案”,“置信度”和“模糊”中的值。请注意,对所有非分组变量的列都执行了反转,因此它应可缩放到任意数量的列。按“ Q_ID”分组可能不是必需的,但是您的问题在这一点上还不清楚。

library(tidyverse)
library(magrittr)

dat.partner <- dat %>% 
  group_by(Q_ID, TeamID) %>% 
  mutate_all(rev) %>% 
  ungroup %>% 
  select(-Q_ID, -TeamID) %>% 
  set_colnames(paste0('partner_', colnames(.)))

dat.final <- cbind(dat, dat.partner)

  Q_ID TeamID     Answer Confidence Blur partner_TeamID partner_Answer partner_Confidence partner_Blur
1    1      A        Yes         88    0              A          Maybe                 99            0
2    1      A      Maybe         99    0              A            Yes                 88            0
3    1      B         No         20    1              B            Yes                 22            1
4    1      B        Yes         22    1              B             No                 20            1
5    1      C Definitely         32    2              C            Yes                 55            1
6    1      C        Yes         55    1              C     Definitely                 32            2
7    1      D         No         33    5              D          Maybe                 33            9
8    1      D      Maybe         33    9              D             No                 33            5