soo*_*sus 1 if-statement r string-matching
说我有一个数据名人堂 df
resident faculty submittedBy match caseID phase
george sally george 1 george_1 pre
george sally sally 0 george_1 pre
george sally george 1 george_1 intra
jane carl jane 1 jane_1 pre
jane carl carl 0 jane_1 pre
jane carl carl 0 jane_1 intra
Run Code Online (Sandbox Code Playgroud)
并且我想df$response根据以下参数在此数据框中添加一列(我认为我需要一组嵌套的ifelses,但是我正在努力正确地执行它):
对于给定的X行,如果df$match= 1,
在以下情况下打印“ 1” df$response:
任何行中df$match,其中df$match= 0具有在相同的内容df$caseID,df$faculty以及df$phase作为列X.否则打印“0”。
所以输出应该是这样的:
response
1
0
0
1
0
0
Run Code Online (Sandbox Code Playgroud)
因为只有第一和第四行包含值,其中有在比赛df$caseID,df$faculty以及df$phase两个一排,其中df$match= 1和行,其中df$match= 0。
我们可以使用data.table方法。将'data.frame'转换为'data.table'(setDT(df1)),按'caseID','faculty','phase'分组,获取检查unique元素的长度match是否等于2并创建一个二进制列('对于'match'为0'的值,请将'response'分配为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
Run Code Online (Sandbox Code Playgroud)
或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
Run Code Online (Sandbox Code Playgroud)
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))
Run Code Online (Sandbox Code Playgroud)