jnshsrs
jnshsrs

Reputation: 337

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.

Upvotes: 0

Views: 114

Answers (2)

MetehanGungor
MetehanGungor

Reputation: 171

You can manage it with 3 functions: c(), ifelse() and cbind()

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)
agreement <- ifelse(rater_A == rater_B, 1, 0)
cbind(rater_A, rater_B, agreement) 

Upvotes: 0

nrussell
nrussell

Reputation: 18602

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

Upvotes: 1

Related Questions