【问题标题】:Add a row to beginning of each group in a data frame using dplyr使用 dplyr 在数据框中的每个组的开头添加一行
【发布时间】:2020-03-30 02:02:09
【问题描述】:

我希望有一个快速的问题,但很难弄清楚我想做什么。我想:

  1. 保留数据中存在的分组
  2. 在每个组中添加一行,其中“value”的值为 0,“months in period”作为该组的第一行

因此,对于 my_group、value、months_in_period,新数据框的第一行将是 1、0、0。当我们点击 my_group = 3 时,第一行将是 3, 0, 0 for my_group, value, months_in_period

我看到这个问题here 非常相似,但不同之处在于我想为数据框中的每个分组添加一行。

示例数据

df_example <- 
structure(list(my_group = c(1L, 1L, 1L, 1L, 3L, 3L, 3L, 5L, 5L, 
5L, 7L, 7L, 7L), value = c(0.11, -1.27, -1.61, -0.59, -0.56, 
-2.06, -2.53, 0.98, -0.06, -2.65, -0.54, -0.05, -1.33), months_in_period = c(1L, 
2L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -13L))

# A tibble: 13 x 3
   my_group value months_in_period
      <int> <dbl>            <int>
 1        1  0.11                1
 2        1 -1.27                2
 3        1 -1.61                3
 4        1 -0.59                4
 5        3 -0.56                1
 6        3 -2.06                2
 7        3 -2.53                3
 8        5  0.98                1
 9        5 -0.06                2
10        5 -2.65                3
11        7 -0.54                1
12        7 -0.05                2
13        7 -1.33                3

我试过了,但它显然不起作用:

df_example %>% 
  group_by(my_group) %>%
  rbind(data.frame(my_group = .$my_group, value = 0, months_in_period = 0), .)

感谢任何帮助!

【问题讨论】:

    标签: r dplyr rbind


    【解决方案1】:

    可能不是最好的方法,但您可以将数据转换为宽格式,添加额外的行,然后再次调整为长格式

    library(tidyverse)
    
    # add one extra row to the 1st row
    df1 <- df_example %>% 
      pivot_wider(names_from = my_group,
                  values_from = value) %>% 
      add_row(.before = 0) 
    df1
    #> # A tibble: 5 x 5
    #>   months_in_period   `1`   `3`   `5`   `7`
    #>              <int> <dbl> <dbl> <dbl> <dbl>
    #> 1               NA NA    NA    NA    NA   
    #> 2                1  0.11 -0.56  0.98 -0.54
    #> 3                2 -1.27 -2.06 -0.06 -0.05
    #> 4                3 -1.61 -2.53 -2.65 -1.33
    #> 5                4 -0.59 NA    NA    NA
    
    # assign 0 to the 1st row
    df1[1, ] <- 0
    # convert to long format
    df1 %>% 
      pivot_longer(-months_in_period, 
                   names_to = 'my_group',
                   values_to = "value") %>% 
      select(my_group, value, months_in_period) %>% 
      arrange(my_group, months_in_period) %>% 
      filter(!is.na(value))
    #> # A tibble: 17 x 3
    #>    my_group value months_in_period
    #>    <chr>    <dbl>            <dbl>
    #>  1 1         0                   0
    #>  2 1         0.11                1
    #>  3 1        -1.27                2
    #>  4 1        -1.61                3
    #>  5 1        -0.59                4
    #>  6 3         0                   0
    #>  7 3        -0.56                1
    #>  8 3        -2.06                2
    #>  9 3        -2.53                3
    #> 10 5         0                   0
    #> 11 5         0.98                1
    #> 12 5        -0.06                2
    #> 13 5        -2.65                3
    #> 14 7         0                   0
    #> 15 7        -0.54                1
    #> 16 7        -0.05                2
    #> 17 7        -1.33                3
    

    reprex package (v0.3.0) 于 2020 年 3 月 29 日创建

    【讨论】:

      猜你喜欢
      • 2017-09-10
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      相关资源
      最近更新 更多