在tidyverse中,根据现有变量创建seq()列

问题描述 投票:0回答:2

目标:在tidyverse内,创建一个名为my_seq的序列列。每个seq()编号应使用“from”(x列)和“to”(y列)的现有列。

自我指称“点”组合的加分点(以及点语法的解释)。

boo <- tribble(
  ~ x,  ~y,
    5,   20,
    6,  10,
    2,   20)

# Desired results should reflect these results in new column:
seq(5, 20, by = 2)
#> [1]  5  7  9 11 13 15 17 19
seq(6, 10, by = 2)
#> [1]  6  8 10
seq(2, 20, by = 2)
#>  [1]  2  4  6  8 10 12 14 16 18 20

# These straightforward solutions do not work

boo %>% 
  mutate(my_seq = seq(x, y, by = 2))

boo %>% 
  mutate(my_seq = seq(boo$x, boo$y, by = 2))

# The grammar of self-referential dots is super arcane, but 
# here are some additional tries. All fail.

boo %>%
  mutate(my_seq = map_int(boo, ~seq(.$x, .$y, by = 2)))

boo %>% 
  mutate(my_seq = seq(.$x, .$y, by = 2))
r dplyr purrr
2个回答
2
投票

使用purrr,你可以使用map2并行循环通过xy,这类似于基础R中的Map/mapply但语法不同:

boo %>% mutate(my_seq = map2(x, y, seq, by=2))
# A tibble: 3 x 3
#      x     y     my_seq
#  <dbl> <dbl>     <list>
#1     5    20  <dbl [8]>
#2     6    10  <dbl [3]>
#3     2    20 <dbl [10]>

my_seq是列表类型的列,我们可以在pull列中查看其内容:

boo %>% mutate(my_seq = map2(x, y, seq, by=2)) %>% pull(my_seq)
#[[1]]
#[1]  5  7  9 11 13 15 17 19

#[[2]]
#[1]  6  8 10

#[[3]]
# [1]  2  4  6  8 10 12 14 16 18 20

1
投票

通常,当有多个参数时,也可以使用pmap

library(dplyr)
library(purrr)
res <- boo %>% 
          mutate(my_seq = pmap(.,  .f = ~seq(..1, ..2, by = 2)))
res 
# A tibble: 3 x 3
#      x     y my_seq    
#   <dbl> <dbl> <list>    
#1  5.00  20.0 <dbl [8]> 
#2  6.00  10.0 <dbl [3]> 
#3  2.00  20.0 <dbl [10]>


res$my_seq
#[[1]]
#[1]  5  7  9 11 13 15 17 19

#[[2]]
#[1]  6  8 10

#[[3]]
#[1]  2  4  6  8 10 12 14 16 18 20
© www.soinside.com 2019 - 2024. All rights reserved.