【问题标题】:Creating sequence of numbers inside a string R在字符串 R 中创建数字序列
【发布时间】:2021-07-19 09:27:29
【问题描述】:

我想创建一个数字序列作为字符串。我有“开始”和“结束”列指示序列的开始和结束。所需的输出是一个序列为 1 的字符串。请参见下面的示例。

df <- data.frame(ID=seq(1:5), 
                 start=seq(2,10,by=2),
                 end=seq(5,13,by=2),
                 desired_output_aschar= c("2,3,4,5", "4,5,6,7", "6,7,8,9", "8,9,10,11", "10,11,12,13"))

View(df)

提前谢谢你...

【问题讨论】:

    标签: r string seq


    【解决方案1】:

    以下解决方案只需要一个*apply 循环。

    mapply(function(x, y) paste(x:y, collapse = ","), df$start, df$end)
    #[1] "2,3,4,5"     "4,5,6,7"     "6,7,8,9"     "8,9,10,11"   "10,11,12,13"
    

    使用新的 lambda,输出相同。

    mapply(\(x, y) paste(x:y, collapse = ","), df$start, df$end)
    

    【讨论】:

    • 并且在结果序列长度不同的情况下它仍然可以工作。
    【解决方案2】:

    maply调用不同的seq函数,sapply调用列

    sapply(
      data.frame(mapply(seq,df$start,df$end)),
      paste0,
      collapse=","
    )
    
               X1            X2            X3            X4            X5 
        "2,3,4,5"     "4,5,6,7"     "6,7,8,9"   "8,9,10,11" "10,11,12,13" 
    

    【讨论】:

      【解决方案3】:

      使用dplyr -

      library(dplyr)
      
      df %>%
        rowwise() %>%
        mutate(output = toString(start:end)) %>%
        ungroup
      
      #     ID start   end output        
      #  <int> <dbl> <dbl> <chr>         
      #1     1     2     5 2, 3, 4, 5    
      #2     2     4     7 4, 5, 6, 7    
      #3     3     6     9 6, 7, 8, 9    
      #4     4     8    11 8, 9, 10, 11  
      #5     5    10    13 10, 11, 12, 13
      

      【讨论】:

        【解决方案4】:

        我们可以使用purrr中的map2

        library(dplyr)
        library(purrr)
        df %>% 
           mutate(output = map2_chr(start, end,  ~ toString(.x:.y)))
          ID start end desired_output_aschar         output
        1  1     2   5               2,3,4,5     2, 3, 4, 5
        2  2     4   7               4,5,6,7     4, 5, 6, 7
        3  3     6   9               6,7,8,9     6, 7, 8, 9
        4  4     8  11             8,9,10,11   8, 9, 10, 11
        5  5    10  13           10,11,12,13 10, 11, 12, 13
        

        【讨论】:

          【解决方案5】:

          data.table 选项

          > setDT(df)[, out := toString(seq(start, end)), ID][]
             ID start end desired_output_aschar            out
          1:  1     2   5               2,3,4,5     2, 3, 4, 5
          2:  2     4   7               4,5,6,7     4, 5, 6, 7
          3:  3     6   9               6,7,8,9     6, 7, 8, 9
          4:  4     8  11             8,9,10,11   8, 9, 10, 11
          5:  5    10  13           10,11,12,13 10, 11, 12, 13
          

          【讨论】:

            猜你喜欢
            • 2017-06-12
            • 1970-01-01
            • 2021-06-08
            • 2020-06-30
            • 2022-06-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-03-08
            相关资源
            最近更新 更多