【问题标题】:Create several dummy variables from one string variable从一个字符串变量创建多个虚拟变量
【发布时间】:2015-05-20 00:51:59
【问题描述】:

我已经尝试了来自this similar question 的几乎所有内容,但我无法获得其他人似乎正在获得的结果。这是我的问题:

我有一个这样的数据框,列出了每个老师的成绩:

> profs <- data.frame(teaches = c("1st", "1st, 2nd",
                                  "2nd, 3rd",
                                  "1st, 2nd, 3rd"))
> profs
        teaches
1           1st
2      1st, 2nd
3      2nd, 3rd
4 1st, 2nd, 3rd

我一直在寻找将teaches 变量分成列的解决方案,如下所示:

  teaches1st teaches2nd teaches3rd
1          1          0          0
2          1          1          0
3          0          1          1
4          1          1          1

I understand this solution 涉及 splitstackshape 库和显然已弃用的 concat.split.expanded 函数应该完全符合我的要求,因为回答者的解释。但是,我似乎无法达到相同的结果:

> concat.split.expanded(profs, "teaches", fill = 0, drop = TRUE)
Fehler in seq.default(min(vec), max(vec)) : 
  'from' cannot be NA, NaN or infinite

使用cSplit,我理解它取代了“大多数早期的 concat.split* 函数”,我明白了:

> cSplit(profs, "teaches")
   teaches_1 teaches_2 teaches_3
1:       1st        NA        NA
2:       1st       2nd        NA
3:       2nd       3rd        NA
4:       1st       2nd       3rd

我已经尝试使用cSplit 的帮助并调整其中的每一个参数,但我就是无法进行拆分。感谢您的帮助。

【问题讨论】:

    标签: r string dataframe split splitstackshape


    【解决方案1】:

    由于您的连接数据是连接字符串(不是连接数值),您需要添加 type = "character" 以使函数按预期工作。

    函数的默认设置是数值,因此出现NaN等错误。

    命名与同一家族中其他函数的缩写形式更加一致。因此,它现在是 cSplit_e(尽管旧的函数名称仍然可以使用)。

    library(splitstackshape)
    cSplit_e(profs, "teaches", ",", type = "character", fill = 0)
    #         teaches teaches_1st teaches_2nd teaches_3rd
    # 1           1st           1           0           0
    # 2      1st, 2nd           1           1           0
    # 3      2nd, 3rd           0           1           1
    # 4 1st, 2nd, 3rd           1           1           1
    

    ?concat.split.expanded 的帮助页面与cSplit_e 的帮助页面相同。如果您有任何使其更清晰易懂的提示,请在包的 GitHub 页面上提出问题。

    【讨论】:

    • 谢谢,我想我太专注于阅读有关 cSplit 的内容,忘记阅读 concat.split.expandedcSplit_e 的帮助。这些帮助文件很清楚,我只希望在?cSplit 有指向这些功能的链接。
    【解决方案2】:

    你可以试试 mtabulateqdapTools

    library(qdapTools)
    res <- mtabulate(strsplit(as.character(profs$teaches), ', '))
    colnames(res) <- paste0('teaches', colnames(res))
    res
    #    teaches1st teaches2nd teaches3rd
    #1          1          0          0
    #2          1          1          0
    #3          0          1          1
    #4          1          1          1
    

    或者使用stringi

    library(stringi)
    (vapply(c('1st', '2nd', '3rd'), stri_detect_fixed, logical(4L), 
                              str=profs$teaches))+0L
    #     1st 2nd 3rd
    #[1,]   1   0   0
    #[2,]   1   1   0
    #[3,]   0   1   1
    #[4,]   1   1   1
    

    【讨论】:

      【解决方案3】:

      这是另一种选择:

      Vectorize(grepl, 'pattern')(c('1st', '2nd', '3rd'), profs$teaches)
      #        1st   2nd   3rd
      # [1,]  TRUE FALSE FALSE
      # [2,]  TRUE  TRUE FALSE
      # [3,] FALSE  TRUE  TRUE
      # [4,]  TRUE  TRUE  TRUE
      

      【讨论】:

        【解决方案4】:

        我找到了解决方法。如果您有一个只包含分隔符和数字的字符串变量,那么concat.split.expanded 似乎可以工作,即:

        > profs <- data.frame(teaches = c("1", "1, 2", "2, 3", "1, 2, 3"))
        > profs
          teaches
        1       1
        2    1, 2
        3    2, 3
        4 1, 2, 3
        

        现在concat.split.expandedDummy variables from a string variable 一样工作:

        > concat.split.expanded(profs, "teaches", fill = 0, drop = TRUE)
          teaches_1 teaches_2 teaches_3
        1         1         0         0
        2         1         1         0
        3         0         1         1
        4         1         1         1
        

        但是,我仍在寻找不涉及从我的 teaches 变量中删除所有字母的解决方案。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-03-02
          • 2021-06-25
          • 1970-01-01
          • 2021-09-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多