这是一个蛮力解决方案:
library(plyr)
fun.2 = function (x) {
x[which(!is.na(match(names(x),strsplit(as.character(x[[2]]),'')[[1 ]][seq(1,length(strsplit(as.character(x[[2]]),'')[[1]]),by=2)])))] = 1
return(x)
}
myfunction = function (df) {
df1 = cbind(df,A=rep(0,nrow(df)),B=rep(0,nrow(df)),C=rep(0,nrow(df)),D=rep(0,nrow(df)),E=rep(0,nrow(df)))
df2 = adply (df1,1,fun.2)
return(df2)
}
# you can run
myfunction ( df )
Date Names A B C D E
1 1/1/2000 A|B 1 1 0 0 0
2 2/3/2003 A|C|D 1 0 1 1 0
3 2/4/2004 B|C|E 0 1 1 0 1