如何根据 R 中的数据框生成多个数字序列?
How to generate multiple number series base on a dataframe in R?
我有一个像这样的数据框
df <- data.frame(start = 1:9, end = seq(2,18,by = 2))
# Now I want to generate multiple series number set base on these 2 columns
# The ideal result should be looks like this way
1,2
2,3,4
3,4,5,6
....
谁能帮我解决这个问题?感谢
看起来我们可以在 Map
的每一行上执行此操作,方法是在 data.frame 上应用 :
,这样左边和右边就是 [= 的相应元素17=]、'end' 即 1:2
、2:4
等
do.call(Map, c(f = `:`, df))
#[[1]]
#[1] 1 2
#[[2]]
#[1] 2 3 4
#[[3]]
#[1] 3 4 5 6
#[[4]]
#[1] 4 5 6 7 8
#[[5]]
#[1] 5 6 7 8 9 10
#[[6]]
#[1] 6 7 8 9 10 11 12
#[[7]]
#[1] 7 8 9 10 11 12 13 14
#[[8]]
#[1] 8 9 10 11 12 13 14 15 16
#[[9]]
# [1] 9 10 11 12 13 14 15 16 17 18
apply(df,1,function(x){c(x[1]:x[2])})
[[1]]
[1] 1 2
[[2]]
[1] 2 3 4
[[3]]
[1] 3 4 5 6
[[4]]
[1] 4 5 6 7 8
[[5]]
[1] 5 6 7 8 9 10
[[6]]
[1] 6 7 8 9 10 11 12
[[7]]
[1] 7 8 9 10 11 12 13 14
[[8]]
[1] 8 9 10 11 12 13 14 15 16
[[9]]
[1] 9 10 11 12 13 14 15 16 17 18
tidyverse 解决方案可能如下所示:
library(dplyr)
res_df <- df %>%
rowwise() %>%
mutate(msn = list(seq(start, end)))
pull(res_df, msn)
#> [[1]]
#> [1] 1 2
#>
#> [[2]]
#> [1] 2 3 4
#>
#> [[3]]
#> [1] 3 4 5 6
#>
#> [[4]]
#> [1] 4 5 6 7 8
#>
#> [[5]]
#> [1] 5 6 7 8 9 10
#>
#> [[6]]
#> [1] 6 7 8 9 10 11 12
#>
#> [[7]]
#> [1] 7 8 9 10 11 12 13 14
#>
#> [[8]]
#> [1] 8 9 10 11 12 13 14 15 16
#>
#> [[9]]
#> [1] 9 10 11 12 13 14 15 16 17 18
或者这个:
library(purrr)
map2(df$start, df$end, seq)
#> [[1]]
#> [1] 1 2
#>
#> [[2]]
#> [1] 2 3 4
#>
#> [[3]]
#> [1] 3 4 5 6
#>
#> [[4]]
#> [1] 4 5 6 7 8
#>
#> [[5]]
#> [1] 5 6 7 8 9 10
#>
#> [[6]]
#> [1] 6 7 8 9 10 11 12
#>
#> [[7]]
#> [1] 7 8 9 10 11 12 13 14
#>
#> [[8]]
#> [1] 8 9 10 11 12 13 14 15 16
#>
#> [[9]]
#> [1] 9 10 11 12 13 14 15 16 17 18
我有一个像这样的数据框
df <- data.frame(start = 1:9, end = seq(2,18,by = 2))
# Now I want to generate multiple series number set base on these 2 columns
# The ideal result should be looks like this way
1,2
2,3,4
3,4,5,6
....
谁能帮我解决这个问题?感谢
看起来我们可以在 Map
的每一行上执行此操作,方法是在 data.frame 上应用 :
,这样左边和右边就是 [= 的相应元素17=]、'end' 即 1:2
、2:4
等
do.call(Map, c(f = `:`, df))
#[[1]]
#[1] 1 2
#[[2]]
#[1] 2 3 4
#[[3]]
#[1] 3 4 5 6
#[[4]]
#[1] 4 5 6 7 8
#[[5]]
#[1] 5 6 7 8 9 10
#[[6]]
#[1] 6 7 8 9 10 11 12
#[[7]]
#[1] 7 8 9 10 11 12 13 14
#[[8]]
#[1] 8 9 10 11 12 13 14 15 16
#[[9]]
# [1] 9 10 11 12 13 14 15 16 17 18
apply(df,1,function(x){c(x[1]:x[2])})
[[1]]
[1] 1 2
[[2]]
[1] 2 3 4
[[3]]
[1] 3 4 5 6
[[4]]
[1] 4 5 6 7 8
[[5]]
[1] 5 6 7 8 9 10
[[6]]
[1] 6 7 8 9 10 11 12
[[7]]
[1] 7 8 9 10 11 12 13 14
[[8]]
[1] 8 9 10 11 12 13 14 15 16
[[9]]
[1] 9 10 11 12 13 14 15 16 17 18
tidyverse 解决方案可能如下所示:
library(dplyr)
res_df <- df %>%
rowwise() %>%
mutate(msn = list(seq(start, end)))
pull(res_df, msn)
#> [[1]]
#> [1] 1 2
#>
#> [[2]]
#> [1] 2 3 4
#>
#> [[3]]
#> [1] 3 4 5 6
#>
#> [[4]]
#> [1] 4 5 6 7 8
#>
#> [[5]]
#> [1] 5 6 7 8 9 10
#>
#> [[6]]
#> [1] 6 7 8 9 10 11 12
#>
#> [[7]]
#> [1] 7 8 9 10 11 12 13 14
#>
#> [[8]]
#> [1] 8 9 10 11 12 13 14 15 16
#>
#> [[9]]
#> [1] 9 10 11 12 13 14 15 16 17 18
或者这个:
library(purrr)
map2(df$start, df$end, seq)
#> [[1]]
#> [1] 1 2
#>
#> [[2]]
#> [1] 2 3 4
#>
#> [[3]]
#> [1] 3 4 5 6
#>
#> [[4]]
#> [1] 4 5 6 7 8
#>
#> [[5]]
#> [1] 5 6 7 8 9 10
#>
#> [[6]]
#> [1] 6 7 8 9 10 11 12
#>
#> [[7]]
#> [1] 7 8 9 10 11 12 13 14
#>
#> [[8]]
#> [1] 8 9 10 11 12 13 14 15 16
#>
#> [[9]]
#> [1] 9 10 11 12 13 14 15 16 17 18