9

我有一个数据框,其中包含一个包含字符串列表的分类变量,长度可变(这很重要,因为否则这个问题将与thisthis重复),例如:

df <- data.frame(x = 1:5)
df$y <- list("A", c("A", "B"), "C", c("B", "D", "C"), "E")
df
  x       y
1 1       A
2 2    A, B
3 3       C
4 4 B, D, C
5 5       E

所需的形式是在 中任何地方看到的每个唯一字符串的虚拟变量df$y,即:

data.frame(x = 1:5, A = c(1,1,0,0,0), B = c(0,1,0,1,0), C = c(0,0,1,1,0), D = c(0,0,0,1,0), E = c(0,0,0,0,1))
  x A B C D E
1 1 1 0 0 0 0
2 2 1 1 0 0 0
3 3 0 0 1 0 0
4 4 0 1 1 1 0
5 5 0 0 0 0 1

这种天真的方法有效:

> uniqueStrings <- unique(unlist(df$y))
> n <- ncol(df)
> for (i in 1:length(uniqueStrings)) {
+   df[,  n + i] <- sapply(df$y, function(x) ifelse(uniqueStrings[i] %in% x, 1, 0))
+   colnames(df)[n + i] <- uniqueStrings[i]
+ }

然而,大数据帧非常丑陋、懒惰和缓慢。

有什么建议么?有什么花哨的tidyverse


更新:我在下面得到了 3 种不同的方法。system.time我在我的(Windows 7、32GB RAM)笔记本电脑上使用真实数据集对它们进行了测试,该数据集由 1M 行组成,每行包含长度为 1 到 4 个字符串(约 350 个唯一字符串值中)的列表,磁盘上总共有 200MB。所以预期的结果是一个尺寸为 1M x 350 的数据框。tidyverse(@Sotos) 和base(@joel.wilson) 方法花了很长时间,我不得不重新启动 R。qdapTools(@akrun) 方法非常有效:

> system.time(res1 <- mtabulate(varsLists))
   user  system elapsed 
  47.05   10.27  116.82

所以这是我将标记为接受的方法。

4

3 回答 3

7

另一个想法,

library(dplyr)
library(tidyr)

df %>% 
 unnest(y) %>% 
 mutate(new = 1) %>% 
 spread(y, new, fill = 0) 

#  x A B C D E
#1 1 1 0 0 0 0
#2 2 1 1 0 0 0
#3 3 0 0 1 0 0
#4 4 0 1 1 1 0
#5 5 0 0 0 0 1

除了您在评论中提到的情况,我们可以使用dcastfromreshape2因为它比spread,

df2 <- df %>% 
        unnest(y) %>% 
        group_by(x) %>% 
        filter(!duplicated(y)) %>% 
        ungroup()

reshape2::dcast(df2, x ~ y, value.var = 'y', length)

#  x A B C D E
#1 1 1 0 0 0 0
#2 2 1 1 0 0 0
#3 3 0 0 1 0 0
#4 4 0 1 1 1 0
#5 5 0 0 0 0 1

#or with df$x <- c(1, 1, 2, 2, 3)

#  x A B C D E
#1 1 1 1 0 0 0
#2 2 0 1 1 1 0
#3 3 0 0 0 0 1

#or with df$x <- rep(1,5)

#  x A B C D E
#1 1 1 1 1 1 1
于 2017-01-16T09:21:13.017 回答
6

我们可以用mtabulate

library(qdapTools)
cbind(df[1], mtabulate(df$y))
#  x A B C D E
#1 1 1 0 0 0 0
#2 2 1 1 0 0 0
#3 3 0 0 1 0 0
#4 4 0 1 1 1 0
#5 5 0 0 0 0 1
于 2017-01-16T08:59:04.647 回答
2

这不涉及外部软件包,

# thanks to Sotos for suggesting to use `unique(unlist(df$y))` instead of `LETTERS[1!:5]`
sapply(unique(unlist(df$y)), function(j) as.numeric(grepl(j, df$y)))
#     A B C D E
#[1,] 1 0 0 0 0
#[2,] 1 1 0 0 0
#[3,] 0 0 1 0 0
#[4,] 0 1 1 1 0
#[5,] 0 0 0 0 1
于 2017-01-16T09:29:22.490 回答