I have a df
like this:
df <- data.frame(FOO = c(\'A|B|C\', \'A|B\', \'B|C\', \'A\', \'C\'))
> df
FOO
1 A|B|C
2 A|B
3 B|C
4 A
5
Use unique
and strsplit
to find all unique values (A
, B
and C
in this case). Use grep
to search for the unique values, and return the values when there's a match or character(0)
otherwise. cbind
the resulting characters. Use apply
and ifelse
to replace character(0)
with NA
.
vals <- unique(unlist(sapply(a1, function(x) strsplit(x, '|', fixed = T))))
out <- NULL
for(i in vals){
out <- cbind(out, as.character((lapply(df$FOO, function(x) grep(x, i, value=T)))))
}
apply(out, 2, function(x) ifelse(x=="character(0)", NA, x))
[,1] [,2] [,3]
[1,] "A" "B" "C"
[2,] "A" "B" NA
[3,] NA "B" "C"
[4,] "A" NA NA
[5,] NA NA "C"