【问题标题】:regex to find every two commas, to separate rows from a column using dplyr正则表达式查找每两个逗号,使用 dplyr 将行与列分开
【发布时间】:2021-11-02 01:48:19
【问题描述】:

我有这样的字符串:

71,72,80,81,102,100

我想分隔每 2 个“数字”,所以:

71,72
80,81
102,100

我写了这个正则表达式:

(([0-9]{1,4}),([0-9]{1,4}))

突出显示我需要的组,除了“,”之间的逗号

在我的代码中,我使用的是dplyr

例子:

df_example <- tibble(Lotes= "LOT1,LOT2,LOT3",NoModuloPlastico = "71,72,80,81,102,100")

df_result_example <- df_example %>%
separate_rows(c(Lotes),sep=",") %>%
separate_rows(c(NoModuloPlastico),sep="(([0-9]{1,3}),([0-9]{1,3}))")

这意味着我真正需要的是用正则表达式突出显示每 2 个逗号,但我找不到方法。

我无法根据我的需要调整这些链接:

https://bedigit.com/blog/regex-how-to-match-everything-except-a-particular-pattern/

https://blog.codinghorror.com/excluding-matches-with-regular-expressions/

我得到了什么:

Lotes NoModuloPlastico
LOT1 ""
LOT1 ","
LOT1 ","
LOT1 ""
LOT2 ""
LOT2 ","
LOT2 ","
LOT2 ""
LOT3 ""
LOT3 ","
LOT3 ","
LOT3 ""

我想要什么:

Lotes NoModuloPlastico
LOT1 71,72
LOT2 80,81
LOT3 102,100

【问题讨论】:

    标签: r regex dataframe dplyr separator


    【解决方案1】:

    你可以这样做:

    df_example %>%
      mutate(Lotes = str_split(Lotes, ','),
             NoModuloPlastico = NoModuloPlastico %>%
               str_replace_all('([^,]+,[^,]+),', '\\1:') %>%
               str_split(':')) %>%
      unnest(everything())
    
    # A tibble: 3 x 2
      Lotes NoModuloPlastico
      <chr> <chr>           
    1 LOT1  71,72           
    2 LOT2  80,81           
    3 LOT3  102,100
    

    【讨论】:

    • 鼓舞人心的一如既往:)虽然已经投票了。
    【解决方案2】:

    你可以使用略短的Onyambu's solution

    df_example %>% 
      mutate(Lotes = strsplit(Lotes, ','),
        NoModuloPlastico = NoModuloPlastico %>% 
          strsplit('[^,]*,[^,]*\\K,', perl=TRUE)) %>% 
      unnest(everything())
    

    输出:

    # A tibble: 3 x 2
      Lotes NoModuloPlastico
      <chr> <chr>           
    1 LOT1  71,72           
    2 LOT2  80,81           
    3 LOT3  102,100 
    

    注意事项

    • strsplit(Lotes, ',') 用逗号分隔 Lotes
    • strsplit('[^,]*,[^,]*\\K,', perl=TRUE)NoModuloPlastico 列与其他逗号分开。 [^,]*,[^,]* 匹配零个或多个非逗号字符,一个逗号和零个或多个非逗号字符,\K 忽略这些匹配的字符,然后, 匹配一个用于分割字符串的逗号。李>

    【讨论】:

    • 这太棒了,亲爱的维克托 :)
    【解决方案3】:

    我不知道这是否概括了您的问题,如果没有,请尝试找到此方法的变体。

    # Your tibble
    df_example  = dplyr::tibble(Lotes= "LOT1,LOT2,LOT3",NoModuloPlastico = "71,72,80,81,102,100")
    
    # with strsplit separate the strings by "," and turn them into a matrix
    A = matrix(strsplit(df_example$Lotes, split = ",")[[1]],ncol=1,)
    B = matrix(strsplit(df_example$NoModuloPlastico, split = ",")[[1]], ncol = 2)
    
    # cbind the two matrices, turn that into a dataframe and give names to the columns
    C = as.data.frame(cbind(A,B))
    colnames(C) = c("Lotes", "Modulo1", "Modulo2")
    
    # Create your new column with paste0() function
    C$NoModuloPlastico = paste0(C$Modulo1, ",",C$Modulo2)
    
    # This is extra but only for following your variable name create that with the two columns.
    df_result_example = data.frame(C$Lotes, C$NoModuloPlastico)
    

    这解决了您在基础 R 中的示例。

    【讨论】:

      【解决方案4】:

      您可以将每隔一个出现的逗号替换为分号(或任何其他分隔符),也可以将NoModuloPlastico 中的逗号值更改为分号并使用separate_rows

      library(dplyr)
      library(tidyr)
      
      df_example %>%
        mutate(NoModuloPlastico = gsub('(,.*?),', '\\1;', NoModuloPlastico), 
               Lotes = gsub(',', ';', Lotes, fixed = TRUE)) %>%
        separate_rows(Lotes, NoModuloPlastico, sep = ';')
      
      #  Lotes NoModuloPlastico
      #  <chr> <chr>           
      #1 LOT1  71,72           
      #2 LOT2  80,81           
      #3 LOT3  102,100         
      

      【讨论】:

        【解决方案5】:

        您也可以使用以下解决方案:

        library(dplyr)
        library(tidyr)
        
        df_example %>%
          mutate(NoModuloPlastico = paste0(regmatches(NoModuloPlastico, gregexpr("\\d+,\\d+", NoModuloPlastico))[[1]],
                                           collapse = " "), 
                 Lotes = gsub(",", " ", Lotes)) %>%
          separate_rows(everything(), sep = "\\s+")
        
        # A tibble: 3 x 2
          Lotes NoModuloPlastico
          <chr> <chr>           
        1 LOT1  71,72           
        2 LOT2  80,81           
        3 LOT3  102,100   
        

        【讨论】:

          猜你喜欢
          • 2015-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多