Eric Green Eric Green -4 years ago 49
R Question

match data across rows for paired ratings

I have a dataframe with pairs of individuals who rated themselves (1,0) and rated their partner (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


I want to compare how someone rated themselves (
rateSelf
) to how they were rated by the other person (
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


What's a smart way to get
ratedByOther
?

Answer Source

I think this does what you want:

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download