【问题标题】:create a numeric list based on another column in tidyverse根据 tidyverse 中的另一列创建一个数字列表
【发布时间】:2023-02-11 01:49:10
【问题描述】:

目标:我想根据另一列中的起始值创建一个数字列表(序号),然后取消嵌套。

在下面的数据中,嵌套的 c(1:10) 序列将从该列开始序号.

x <- tibble(name = c('boo', 'bah', 'bee'),
                seqnbr = c(1,2,3))

A tibble: 3 x 2
  name  seqnbr
  <chr>  <dbl>
1 boo        1
2 bah        2
3 bee        3

期望的结果如下所示:

# A tibble: 3 x 3
  name  seqnbr my_list
  <chr>  <dbl> <chr>  
1 boo        1 list(c(1:10) )
2 bah        2 list(c(2:10) )
3 bee        3 list(c(3:10) )

在这一步之后,我会 unnest() 使值明确。

首选 Tidyverse/purr 解决方案。

【问题讨论】:

    标签: r tidyverse


    【解决方案1】:
    library(tidyverse)
    x <- tibble(name = c('boo', 'bah', 'bee'),
                seqnbr = c(1,2,3))
    
    (x_1 <- x |> rowwise() |> mutate(my_list= list(seq(from=seqnbr,
                                   to=10))))
    
    (x_2 <- unnest_longer(x_1,col=my_list))
    

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 2022-09-23
      • 2023-02-23
      • 1970-01-01
      相关资源
      最近更新 更多