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

时间:2017-12-29 23:25:51

标签: r dplyr purrr

目标:在tidyverse中,创建一个名为my_seq的序列列。每个seq()编号都应使用"来自"的现有列。 (x列)和"到" (y栏)。

自我指涉的奖励积分" dot"组合(和点语法的解释)。

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))

2 个答案:

答案 0 :(得分: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 :(得分: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