在字符串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)

提前谢谢。。。

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

有了新的lambdas,同样的产量。

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

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" 

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

最新更新