我有一对数据框,其中包含自我评分(1,0)并评定其合作伙伴(1,0)的个人对。
have <- data.frame(group=c(1, 1, 2, 2, 3, 3),
person=c(1, 2, 1, 2, 1, 2),
rateSelf=c(1, 0, 1, 0, 1, 0),
rateOther=c(1, 1, 1, 0, 0, 0))
have
# group person rateSelf rateOther
#1 1 1 1 1
#2 1 2 0 1
#3 2 1 1 1
#4 2 2 0 0
#5 3 1 1 0
#6 3 2 0 0
我想比较某人如何评价自己(rateSelf
)对他们如何评价他人(ratedByOther
)。
want <- data.frame(group=c(1, 1, 2, 2, 3, 3),
person=c(1, 2, 1, 2, 1, 2),
rateSelf=c(1, 0, 1, 0, 1, 0),
rateOther=c(1, 1, 1, 0, 0, 0),
ratedByOther=c(1, 1, 0, 1, 0, 0))
want
# group person rateSelf rateOther ratedByOther
#1 1 1 1 1 1
#2 1 2 0 1 1
#3 2 1 1 1 0
#4 2 2 0 0 1
#5 3 1 1 0 0
#6 3 2 0 0 0
获得ratedByOther
的明智之举是什么?
答案 0 :(得分:1)
我认为这可以做你想要的:
library(dplyr)
want <- have %>%
group_by(group) %>%
mutate(ratedByOther = rev(rateOther))
group person rateSelf rateOther ratedByOther
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1 1 1 1 1
2 1 2 0 1 1
3 2 1 1 1 0
4 2 2 0 0 1
5 3 1 1 0 0
6 3 2 0 0 0
答案 1 :(得分:0)
我们可以使用data.table
library(data.table)
setDT(have)[, ratedByOther := rev(rateOther), group]