简体   繁体   English

如何创建一个指示两个二分变量一致的变量

[英]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. 我想创建一个包含1和0的新变量。1代表评估者(两个评估者1或两个评估者0)之间的一致,而0代表不一致。

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 : 您可以将df创建为data.frame (而不是使用cbind ),并within ifelseifelse使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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