Here's a couple ways to do this with plyr
or data.table
.
all_ethnicities <- unique(c(
unlist(strsplit(df$ethnicity, " ")),
unlist(strsplit(df$ethnicity_other, " "))
))
df$id <- 1:nrow(df)
library(plyr)
ddply(df, .(id), function(x)
table(factor(unlist(strsplit(paste(x$ethnicity, x$ethnicity_other), " ")),
levels = all_ethnicities)))
## id ngoni bemba lozi tonga other tongi luvale
## 1 1 1 0 0 0 0 0 0
## 2 2 0 1 0 0 0 0 0
## 3 3 0 0 1 1 0 0 1
## 4 4 0 1 0 1 1 0 0
## 5 5 0 1 0 0 0 1 0
library(data.table)
DT <- data.table(df)
DT[, {
as.list(
table(
factor(
unlist(strsplit(paste(ethnicity, ethnicity_other), " ")),
levels = all_ethnicities)
),
)
}, by = id]
## id ngoni bemba lozi tonga other tongi luvale
## 1: 1 1 0 0 0 0 0 0
## 2: 2 0 1 0 0 0 0 0
## 3: 3 0 0 1 1 0 0 1
## 4: 4 0 1 0 1 1 0 0
## 5: 5 0 1 0 0 0 1 0