Мы можем использовать data.table
методы.Преобразовать data.frame в data.table (setDT(df1)
), сгруппированные по caseID, faculty, phase, получить длину unique
элементов match
, проверить, равна ли она2 и создайте двоичный столбец («ответ»). Для значений, где «совпадение» равно 0 », присвойте« ответ »0
library(data.table)
setDT(df1)[, response := +((uniqueN(match) == 2) & match != 0),
.(caseID, faculty, phase)][]
# resident faculty submittedBy match caseID phase response
#1: george sally george 1 george_1 pre 1
#2: george sally sally 0 george_1 pre 0
#3: george sally george 1 george_1 intra 0
#4: jane carl jane 1 jane_1 pre 1
#5: jane carl carl 0 jane_1 pre 0
#6: jane carl carl 0 jane_1 intra 0
или используя base R
с ave
with(df1,+( match != 0 & ave(match, caseID, faculty, phase,
FUN = function(x) length(unique(x))) == 2))
#[1] 1 0 0 1 0 0
данные
df1 <- structure(list(resident = structure(c(1L, 1L, 1L, 2L, 2L, 2L),
.Label = c("george",
"jane"), class = "factor"), faculty = structure(c(2L, 2L, 2L,
1L, 1L, 1L), .Label = c("carl", "sally"), class = "factor"),
submittedBy = structure(c(2L, 4L, 2L, 3L, 1L, 1L), .Label = c("carl",
"george", "jane", "sally"), class = "factor"), match = c(1L,
0L, 1L, 1L, 0L, 0L), caseID = structure(c(1L, 1L, 1L, 2L,
2L, 2L), .Label = c("george_1", "jane_1"), class = "factor"),
phase = structure(c(2L, 2L, 1L, 2L, 2L, 1L), .Label = c("intra",
"pre"), class = "factor")), class = "data.frame", row.names = c(NA,
-6L))