简体   繁体   中英

How to create a variable that indicates agreement from two dichotomous variables

I d like to create a new variable that contains 1 and 0. A 1 represents agreement between the rater (both raters 1 or both raters 0) and a zero represents disagreement.

rater_A <- c(1,0,1,1,1,0,0,1,0,0)
rater_B <- c(1,1,0,0,1,1,0,1,0,0)
df <- cbind(rater_A, rater_B)

The new variable would be like the following vector I created manually:

df$agreement <- c(1,0,0,0,1,0,1,1,1,1)

Maybe there's a package or a function I don't know. Any help would be great.

You could create df as a data.frame (instead of using cbind ) and use within and ifelse :

rater_A <- c(1,0,1,1,1,0,0,1,0,0)
rater_B <- c(1,1,0,0,1,1,0,1,0,0)
df <- data.frame(rater_A, rater_B)
##
df <- within(df,
  agreement <- ifelse(
    rater_A==rater_B,1,0))
##
> df
   rater_A rater_B agreement
1        1       1         1
2        0       1         0
3        1       0         0
4        1       0         0
5        1       1         1
6        0       1         0
7        0       0         1
8        1       1         1
9        0       0         1
10       0       0         1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM