我有一个数据框,其中一列是列表,如下所示:
>head(movies$genre_list)
[[1]]
[1] "drama" "action" "romance"
[[2]]
[1] "crime" "drama"
[[3]]
[1] "crime" "drama" "mystery"
[[4]]
[1] "thriller" "indie"
[[5]]
[1] "thriller"
[[6]]
[1] "drama" "family"
我想将这一列转换为多列,一列用于列表中的每个唯一元素(在本例中为类型),并将它们作为二进制列。我正在寻找一个优雅的解决方案,它不涉及首先找出有多少类型,然后为每个类型创建一个列,然后检查每个列表元素然后填充流派列。我尝试取消列表,但它不能以我想要的方式使用列表向量。
谢谢!
答案 0 :(得分:4)
以下是一些方法:
movies <- data.frame(genre_list = I(list(
c("drama", "action", "romance"),
c("crime", "drama"),
c("crime", "drama", "mystery"),
c("thriller", "indie"),
c("thriller"),
c("drama", "family"))))
您可以使用“qdapTools”中的mtabulate
功能或我的“splitstackshape”包中未导出的charMat
功能。
语法将是:
library(qdapTools)
mtabulate(movies$genre_list)
# action crime drama family indie mystery romance thriller
# 1 1 0 1 0 0 0 1 0
# 2 0 1 1 0 0 0 0 0
# 3 0 1 1 0 0 1 0 0
# 4 0 0 0 0 1 0 0 1
# 5 0 0 0 0 0 0 0 1
# 6 0 0 1 1 0 0 0 0
或
splitstackshape:::charMat(movies$genre_list, fill = 0)
# action crime drama family indie mystery romance thriller
# [1,] 1 0 1 0 0 0 1 0
# [2,] 0 1 1 0 0 0 0 0
# [3,] 0 1 1 0 0 1 0 0
# [4,] 0 0 0 0 1 0 0 1
# [5,] 0 0 0 0 0 0 0 1
# [6,] 0 0 1 1 0 0 0 0
改进了选项1 :直接使用table
:
table(rep(1:nrow(movies), sapply(movies$genre_list, length)),
unlist(movies$genre_list, use.names=FALSE))
改进了选项2 :使用for
循环。
x <- unique(unlist(movies$genre_list, use.names=FALSE))
m <- matrix(0, ncol = length(x), nrow = nrow(movies), dimnames = list(NULL, x))
for (i in 1:nrow(m)) {
m[i, movies$genre_list[[i]]] <- 1
}
m
以下是旧答案
将列表转换为table
的列表(转而转换为data.frame
s):
tables <- lapply(seq_along(movies$genre_list), function(x) {
temp <- as.data.frame.table(table(movies$genre_list[[x]]))
names(temp) <- c("Genre", paste("Record", x, sep = "_"))
temp
})
使用Reduce
与merge
结果列表。如果我正确理解了您的最终目标,则会产生您感兴趣的结果的转置形式。
merged_tables <- Reduce(function(x, y) merge(x, y, all = TRUE), tables)
merged_tables
# Genre Record_1 Record_2 Record_3 Record_4 Record_5 Record_6
# 1 action 1 NA NA NA NA NA
# 2 drama 1 1 1 NA NA 1
# 3 romance 1 NA NA NA NA NA
# 4 crime NA 1 1 NA NA NA
# 5 mystery NA NA 1 NA NA NA
# 6 indie NA NA NA 1 NA NA
# 7 thriller NA NA NA 1 1 NA
# 8 family NA NA NA NA NA 1
将NA
转置并转换为0
非常简单。只需删除第一列,然后将其重新用作新names
data.frame
movie_genres <- setNames(data.frame(t(merged_tables[-1])), merged_tables[[1]])
movie_genres[is.na(movie_genres)] <- 0
movie_genres
答案 1 :(得分:3)
使用与其他回复相同的输入是一些替代方案:
1) factor / table / rbind
> levs <- levels(factor(unlist(movies[[1]])))
> as.data.frame(do.call(rbind, lapply(lapply(movies[[1]], factor, levs), table)))
action crime drama family indie mystery romance thriller
1 1 0 1 0 0 0 1 0
2 0 1 1 0 0 0 0 0
3 0 1 1 0 0 1 0 0
4 0 0 0 0 1 0 0 1
5 0 0 0 0 0 0 0 1
6 0 0 1 1 0 0 0 0
2) make.groups / xtabs
> library(lattice)
> m <- do.call(make.groups, movies[[1]])
> as.data.frame.matrix(xtabs(~ which + data, m))
action crime drama family indie mystery romance thriller
c("drama", "action", "romance") 1 0 1 0 0 0 1 0
c("crime", "drama") 0 1 1 0 0 0 0 0
c("crime", "drama", "mystery") 0 1 1 0 0 1 0 0
c("thriller", "indie") 0 0 0 0 1 0 0 1
thriller 0 0 0 0 0 0 0 1
c("drama", "family") 0 0 1 1 0 0 0 0
2a) make.groups / dcast 这是替代2的变体,使用reshape2中的dcast
代替as.data.frame.matrix
和xtabs
。融合数据框m
来自备选方案2。
library(reshape2)
dcast(m, which ~ data, fun.aggregate = length, value.var = "which")
更新:添加了备选方案2。
更新2:增加了备选方案2a。