从连续列创建虚拟矩阵

时间:2018-06-08 08:28:28

标签: r dummy-variable

我使用R而我有一个如下所示的列:

relative
aunt
mother,grandmother

sister,mother

我想要的结果应该是这样的:

mother  sister aunt grandmother
0       0      1    0
1       0      0    1
0       0      0    0
1       1      0    0

我该怎么做?提前谢谢。

1 个答案:

答案 0 :(得分:1)

你可以这样做:

relative <- c("aunt", "mother,grandmother", "sister,mother", "", "other")
R <- strsplit(relative, ',')
r <- unique(unlist(R))
result <- t(sapply(R, function(Ri) if (length(Ri)==0) rep(FALSE, length(r)) else r %in% Ri))
colnames(result) <- r
result
# > result
#       aunt mother grandmother sister other
# [1,]  TRUE  FALSE       FALSE  FALSE FALSE
# [2,] FALSE   TRUE        TRUE  FALSE FALSE
# [3,] FALSE   TRUE       FALSE   TRUE FALSE
# [4,] FALSE  FALSE       FALSE  FALSE FALSE
# [5,] FALSE  FALSE       FALSE  FALSE  TRUE

或(对于整数):

+result
# > +result
#      aunt mother grandmother sister other
# [1,]    1      0           0      0     0
# [2,]    0      1           1      0     0
# [3,]    0      1           0      1     0
# [4,]    0      0           0      0     0
# [5,]    0      0           0      0     1