add_row with group_by nest tibble

时间:2017-08-11 15:40:21

标签: r dplyr purrr tibble

我正在尝试将add_row()分组数据而不使用使用do。

library(dplyr)
library(tidyr)
library(purrr)
library(tibble)


my.data <- data.frame(

  supplier = c("a","a","a","a","a","a","b","b","b","b","b","b"),
  date = rep(c("2017-06-01","2017-03-01","2017-02-01","2017-01-12",
               "2017-05-01","2017-04-01"), 2), 
  order = c(1,0,0,1,1,0,0,1,0,0,1,0)

)

解决方案

my.data %>%
  group_by(supplier) %>% 
  do(add_row(.,.before=0))

给出了

# A tibble: 14 x 3
# Groups:   supplier [3]
   supplier       date order
      <chr>      <chr> <dbl>
 1     <NA>       <NA>    NA
 2        a 2017-06-01     1
 3        a 2017-03-01     0
 4        a 2017-02-01     0
 5        a 2017-01-12     1
 6        a 2017-05-01     1
 7        a 2017-04-01     0
 8     <NA>       <NA>    NA
 9        b 2017-06-01     0
10        b 2017-03-01     1
11        b 2017-02-01     0
12        b 2017-01-12     0
13        b 2017-05-01     1
14        b 2017-04-01     0

尝试使用nest和mutate或purrr :: map

my.data %>%
  group_by(supplier) %>%
  nest() %>%
  mutate(extra.row = add_row(data, .before = 0))

mutate_impl(.data,dots)中的错误:   评估错误:不支持的索引类型:NULL。

任何建议。缩放时做的很慢。

0 个答案:

没有答案