【问题标题】:How to combine mutate and str_match correctly?如何正确组合 mutate 和 str_match?
【发布时间】:2021-04-06 07:25:01
【问题描述】:

假设我想将一个字符串列拆分为单独的列。为此,我使用了 stringr 包中的 mutate 和 str_match(或 str_replace),但结果并不理想。

设置数据框并拆分列:

df <-
  data.frame(strings = c('a_b_c', 'ab_cd_ef', 'abc_def_ghi')) %>%
  mutate(string = stringr::str_match(strings, '([a-z]+)_([a-z]+)_([a-z]+)')) 

df
      strings    string.1 string.2 string.3 string.4
1       a_b_c       a_b_c        a        b        c
2    ab_cd_ef    ab_cd_ef       ab       cd       ef
3 abc_def_ghi abc_def_ghi      abc      def      ghi

查看列名时,我只看到两列。这也使得引用列变得复杂。我认为这与 str_match 函数输出的矩阵格式有关。

df %>% ncol
[1] 2

df %>% colnames
[1] "strings" "string"

有没有一种简单的方法可以让这些新列表现得像普通的 data.frame 列?如果可能,使用重命名步骤。这是我想要的东西:

df %>% ncol
[1] 5

df %>% colnames
[1] "strings" "string_1" "string_2" "string_3" "string_4"

df
      strings    string_1 string_2 string_3 string_4
1       a_b_c       a_b_c        a        b        c
2    ab_cd_ef    ab_cd_ef       ab       cd       ef
3 abc_def_ghi abc_def_ghi      abc      def      ghi

【问题讨论】:

    标签: r stringr dplyr


    【解决方案1】:

    我们可以使用cSplit

    library(splitstackshape)
    cSplit(df, "strings", "_", drop = FALSE)
    

    或者使用来自tidyrseparate

    library(tidyr)
    library(stringr)
    df %>%
        separate(strings, into = str_c('string_', 1:3), remove = FALSE)
    

    【讨论】:

      【解决方案2】:

      以最通用的形式回答原始问题:str_match() 生成一个字符矩阵。我们可以使用as_tibble.name_repair 参数将它变成一个tibble 来选择列名——感谢tidyr 魔法,它也可以在mutate() 下工作:

      library(tidyverse)
      
      df <-
        data.frame(strings = c('a_b_c', 'ab_cd_ef', 'abc_def_ghi'))
      
      df %>%
        mutate(stringr::str_match(strings, '([a-z]+)_([a-z]+)_([a-z]+)') %>%
               as_tibble(.name_repair = ~ c("matched", "prefix", "midfix", "suffix")))
      
            strings     matched prefix midfix suffix
      1       a_b_c       a_b_c      a      b      c
      2    ab_cd_ef    ab_cd_ef     ab     cd     ef
      3 abc_def_ghi abc_def_ghi    abc    def    ghi
      

      如果您想丢弃 matched 列(因为在此特定示例中它不会带来任何其他信息 w.r.t. strings),您可以在最后的管道步骤中执行此操作,例如%&gt;% select(-matched),在mutate() 的内部或外部随你喜欢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-24
        • 1970-01-01
        • 2021-06-15
        • 1970-01-01
        • 2023-03-25
        • 2014-12-20
        • 2021-10-15
        相关资源
        最近更新 更多