在字符串 R 中创建数字序列

Creating sequence of numbers inside a string R

我想创建一个数字序列作为字符串。我有“开始”和“结束”列,指示序列的开始和结束。所需的输出是一个序列为 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)

提前谢谢你...

Mapply调用不同的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" 

以下解决方案只需要一个 *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)

使用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

我们可以使用 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

一个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