add_row with group_by nest tibble

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

我正在尝试将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。

有什么建议。缩放时做的很慢。

r dplyr purrr tibble
1个回答
3
投票

您可以使用bind_rows将汇总数据集绑定到原始数​​据集上。

您也可以使用complete,虽然现在您的每组日期相同,并且可能无法按照每组不同日期编写的日期。此外,我相信当你扩大规模时,complete会变慢。

两个解决方案都依赖于date是原始数据集中的实际date变量。

my.data = mutate(my.data, date = as.Date(date) )

summarizebind_rows汇总和约束。 arrange是为了使事情井井有条,在实际案例中很可能不需要。

my.data %>%
    group_by(supplier) %>%
    summarize(date = min(date) - 30) %>%
    bind_rows(., my.data) %>%  
    arrange(supplier, date)

如果组之间的日期相同,则使用complete

my.data %>%
    group_by(supplier) %>%
    complete(date = c(min(.$date) - 30, .$date ) )

两者的结果:

# A tibble: 14 x 3
# Groups:   supplier [2]
   supplier       date order
     <fctr>     <date> <dbl>
 1        a 2016-12-13    NA
 2        a 2017-01-12     1
 3        a 2017-02-01     0
 4        a 2017-03-01     0
 5        a 2017-04-01     0
 6        a 2017-05-01     1
 7        a 2017-06-01     1
 8        b 2016-12-13    NA
 9        b 2017-01-12     0
10        b 2017-02-01     0
11        b 2017-03-01     1
12        b 2017-04-01     0
13        b 2017-05-01     1
14        b 2017-06-01     0
© www.soinside.com 2019 - 2024. All rights reserved.