0

我有一个这样的数据框

   mydf <- data.frame(x = c("a", "b", "q"), 
                      y = c("c", "d", "r"), 
                      min = c(2, 5, 3), 
                      max = c(4,6,7))

        x y min max
        a c   2   4
        b d   5   6
        q r   3   7

我想用一个简单的函数把它扩展到这个

   mydf_expanded <- data.frame(x = c(rep("a",3), rep("b",2), rep("q",5)), 
                               y = c(rep("c",3), rep("d",2), rep("r",5)), 
                               min_to_max = c(2:4,5:6,3:7))

       x y min_to_max
       a c          2
       a c          3
       a c          4
       b d          5
       b d          6
       q r          3
       q r          4
       q r          5
       q r          6
       q r          7

这基本上是试图扩大mydf每一length(mydf$min:mydf:max)行的mydf. 但我也想添加每一行min_to_max中的序列。mydf$min:mydf:max

我知道这个功能可以根据您指定的“计数”进行扩展

       library(splitstackshape)
       mydf_expand = expandRows(mydf, "count")

但这并不能完全让我找到我的解决方案。请帮忙!谢谢!

4

1 回答 1

2
df <- data.frame(x = c("a", "b", "q"), 
                   y = c("c", "d", "r"), 
                   min = c(2, 5, 3), 
                   max = c(4,6,7))
library(tidyverse)

df %>% 
  rowwise() %>% 
  transmute(x, y, all = list(seq(min, max))) %>% 
  unnest_longer(all)
#> # A tibble: 10 x 3
#>    x     y       all
#>    <chr> <chr> <int>
#>  1 a     c         2
#>  2 a     c         3
#>  3 a     c         4
#>  4 b     d         5
#>  5 b     d         6
#>  6 q     r         3
#>  7 q     r         4
#>  8 q     r         5
#>  9 q     r         6
#> 10 q     r         7

reprex 包于 2021-03-11 创建(v1.0.0)

数据表

library(data.table)
setDT(df)

df$all <- apply(df[, c(3,4)], 1, function(x) seq(x[1], x[2]))

df[, list(all = unlist(all)), by = list(x, y)]
#>     x y all
#>  1: a c   2
#>  2: a c   3
#>  3: a c   4
#>  4: b d   5
#>  5: b d   6
#>  6: q r   3
#>  7: q r   4
#>  8: q r   5
#>  9: q r   6
#> 10: q r   7

reprex 包于 2021-03-11 创建(v1.0.0)

于 2021-03-11T14:31:03.217 回答