从串联列创建虚拟矩阵
creating a dummy matrix from a concatenated column
我正在使用 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
我该怎么做?提前致谢。
你可以这样做:
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
我正在使用 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
我该怎么做?提前致谢。
你可以这样做:
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