I have a data frame test:
group userID A_conf A_chall B_conf B_chall
1 220 1 1 1 2
1 222 4 6 4 4
2 223 6 5 3 2
1 224 1 5 4 4
2 228 4 4 4 4
The data contains responses per user (shown by userID) where each user may enter any value between 1 to 6 for both the measures:
- conf
- chall
They can also choose not to respond, resulting in an NA entry.
The test dataframe contains several columns like A, B, C, D and so on. Conf and Chall measures can be reported for each of these columns separately.
I am interested in making following comparisons:
- A_conf & A_chall
- B_conf & B_chall
IF any of these measures are equal, the Final counter should be incremented (as shown below).
group userID A_conf A_chall B_conf B_chall Final
1 220 1 1 1 2 1
1 222 4 6 4 4 1
2 223 6 5 3 2 0
1 224 1 5 4 4 1
2 228 4 4 4 4 2
I am struggling with the Final counter. What script would help me achieve this functionality?
For reference, the dput of the test dataframe set is shared below:
dput(test):
structure(list(group = c(1L, 1L, 2L, 1L, 2L),
userID = c(220L, 222L, 223L, 224L, 228L),
A_conf = c(1L, 4L, 6L, 1L, 4L),
A_chall = c(1L, 6L, 5L, 5L, 4L),
B_conf = c(1L, 4L, 3L, 4L, 4L),
B_chall = c(2L, 4L, 2L, 4L, 4L)),
class = "data.frame", row.names = c(NA, -5L))
I tried a code like this:
test$Final = as.integer(0) # add a column to keep counts
count_inc = as.integer(0) # counter variable to increment in steps of 1
for (i in 1:nrow(test)) {
count_inc = 0
if(!is.na(test$A_conf[i] == test$A_chall[i]))
{
count_inc = 1
test$Final[i] = count_inc
}#if
else if(!is.na(test$A_conf[i] != test$A_chall[i]))
{
count_inc = 0
test$Final[i] = count_inc
}#else if
}#for
The above code has been written to work ONLY on the columns A_conf and A_chall. The problem is, it fills the Final column with all 1's whether the entered values (by users) are equal or not.