I have a small data.frame in which I am trying to compare the columns rowwise.
My df looks like this:
The three columns Coder1, Coder2 and Coder3 inhibit data on which syllable they think is stressed. The column concat is a concatenated version of their answers.
I am trying to add two new columns:
- This column should be named StressAgreeLax.
For this I would need a code which checks whether Coder1 is identical to Coder2, Coder1 to Coder3 or Coder2 to Coder3. And if so: adds the agreed syllable and if not adds "DISAGREE"
e.g. Coder1 Coder2 Coder3 StressAgreeLax
C1 C2 C3 StressAgreeLax
fin fin pen fin
fin pen other DISAGREE
pen fin fin fin
ante pen ante ante
fin fin fin fin
- The second column should be easier. The Column name should be StressAgreeStrict. Here all three coders have to agree with the stressed syllable. Again the agreed stress would need to be put in the new column and if not "DISAGREE".
e.g: Coder1, Coder2, Coder3, StressAgreeLax, StressAgreeStrict
C1 C2 C3 StressAgreeLax StressAgreeStrict
fin fin pen fin DISAGREE
fin pen other DISAGREE DISAGREE
pen fin fin fin DISAGREE
ante pen ante ante DISAGREE
fin fin fin fin fin
This is beyond my R-Knowledge.. I tried various ifelse() combinations and case_when() as well as match(), but nothing worked..
I hope you can help me!
Thanks a lot!
>Solution :
One solution with ifelse could be :
library(dplyr)
data=data.frame(C1=c("fin","fin","pen","ante","fin"),
C2=c("fin","pen","fin","pen","fin"),
C3=c("pen","other","fin","ante","fin"))
data = data %>%
mutate("StressAgreeLax"=ifelse(C1==C2,C1,ifelse(C1==C3,C1,ifelse(C2==C3,C2,"DISAGREE")))) %>%
mutate("StressAgreeStrict"=ifelse(C1==C2 & C2==C3, C1,"DISAGREE"))
data
C1 C2 C3 StressAgreeLax StressAgreeStrict
1 fin fin pen fin DISAGREE
2 fin pen other DISAGREE DISAGREE
3 pen fin fin fin DISAGREE
4 ante pen ante ante DISAGREE
5 fin fin fin fin fin
