【问题标题】:Splitting column of comma separated categories into binary matrix将逗号分隔类别的列拆分为二进制矩阵
【发布时间】:2019-08-05 17:32:38
【问题描述】:

我对 R 很陌生,我真的需要一些帮助。我的数据框中有一列cats,我想将其传播到一个二进制矩阵中,其中 1 是受访者报告感兴趣的地方,如果他们不感兴趣,则为 0。

我发现我的问题与这里的问题非常相似:

Split column of comma-separated numbers into multiple columns based on value

但是,我无法使用上述解决方案解决我的问题,并且在不同的时间点不断收到多个不同的错误。我怀疑这是因为我的数据框包含字符串而不是整数或数字。

这是我正在使用的示例数据框

df <- data.frame(c("sports", "business,IT,entertainment", "feature,entertainment", "business,politics,sports", "health", "politics", "reviews", "entertainment,health", "IT"))

colnames(df) <- "cats"

#                       cats
#1                    sports
#2 business,IT,entertainment
#3     feature,entertainment
#4  business,politics,sports
#5                    health
#6                  politics
#7                   reviews
#8      entertainment,health
#9                        IT

这就是我试图让它看起来像的样子

        sports business IT entertainment politics review health feature    
1         1       0     0        0          0        0      0      0
2         0       1     1        1          0        0      0      0
3         0       0     0        1          0        0      0      1
4         1       1     0        0          1        0      0      0
etc...

我收到的错误示例如下:

Error: row_number() should only be called in a data context

Error in eval_tidy(enquo(var), var_env) : object '' not found

任何帮助将不胜感激!

【问题讨论】:

  • 这正是“splitstackshape”中的cSplit_e 的用途:library(splitstackshape); cSplit_e(df, "cats", ",", type = "character")

标签: r split dplyr tidyverse plyr


【解决方案1】:
+with(df, sapply(unique(unlist(strsplit(as.character(cats), ","))), grepl, cats))
#      sports business IT entertainment feature politics health reviews
# [1,]      1        0  0             0       0        0      0       0
# [2,]      0        1  1             1       0        0      0       0
# [3,]      0        0  0             1       1        0      0       0
# [4,]      1        1  0             0       0        1      0       0
# [5,]      0        0  0             0       0        0      1       0
# [6,]      0        0  0             0       0        1      0       0
# [7,]      0        0  0             0       0        0      0       1
# [8,]      0        0  0             1       0        0      1       0
# [9,]      0        0  1             0       0        0      0       0

【讨论】:

    【解决方案2】:

    mtabulate 的一个选项

    library(qdapTools)
    mtabulate(strsplit(as.character(df$cats), ","))
    #  business entertainment feature health IT politics reviews sports
    #1        0             0       0      0  0        0       0      1
    #2        1             1       0      0  1        0       0      0
    #3        0             1       1      0  0        0       0      0
    #4        1             0       0      0  0        1       0      1
    #5        0             0       0      1  0        0       0      0
    #6        0             0       0      0  0        1       0      0
    #7        0             0       0      0  0        0       1      0
    #8        0             1       0      1  0        0       0      0
    #9        0             0       0      0  1        0       0      0
    

    或与table 来自base R

    table(stack(setNames(strsplit(as.character(df$cats), ","), seq_len(nrow(df))))[2:1])
    

    【讨论】:

      【解决方案3】:

      Based on你可以这样做:

      library(tidyverse)
         df %>% 
        rownames_to_column(var="row") %>% 
        separate_rows(cats, sep=",") %>% 
        count(row, cats) %>% 
        spread(cats, n, fill = 0)
      

      感谢@eipi10编辑

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-06
        • 2018-11-02
        • 2018-09-25
        • 2012-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多