【问题标题】:How to add calculated rows to a tibble?如何将计算的行添加到小标题?
【发布时间】:2021-06-01 05:34:25
【问题描述】:

这是我的小标题。

 A tibble: 5 x 4
  Site     Pre  Post Pct_Change
  <chr>  <dbl> <dbl>      <dbl>
1 Site 1 0.244 0.254       4.1 
2 Site 2 0.209 0.211       0.96
3 Site 3 0.309 0.302      -2.27
4 Site 4 0.315 0.313      -0.63
5 Site 5 0.31  0.316       1.94

我想再添加四行来计算每个列变量(PrePostPct_Change)在Site 1Site 2Site 1Site 3 之间的百分比差异, Site 1Site 4Site 1Site 5。我希望行布局看起来像这样。

 A tibble: 5 x 4
      Site     Pre  Post Pct_Change
      <chr>  <dbl> <dbl>      <dbl>
    1 Site 1 0.244 0.254       4.1 
    2 Site 2 0.209 0.211       0.96
    3 Diff
    4 Site 3 0.315 0.313      -0.63
    5 Diff
    6 Site 4 0.244 0.254       4.1 
    7 Diff
    8 Site 5 0.309 0.302      -2.27
    9 Diff

我对如何对行执行此操作有点困惑,因此非常感谢任何帮助。

【问题讨论】:

  • 您要为每个Site 执行计算吗?我的意思是Site2 vs Site3Site2 vs Site4 等等呢?

标签: r dplyr


【解决方案1】:

如果我的目的正确,我认为您可以使用以下解决方案:

library(dplyr)
library(purrr)

df %>% 
  group_split(Site) %>%
  map_dfr(~ add_row(.x, Site = "Diff", Pre = .x$Pre, Post = .x$Post, 
                    Pct_Change = df$Pct_Change[1] - .x$Pct_Change)) %>%
  filter(Pct_Change != 0)

# A tibble: 9 x 4
  Site     Pre  Post Pct_Change
  <chr>  <dbl> <dbl>      <dbl>
1 Site_1 0.244 0.254       4.1 
2 Site_2 0.209 0.211       0.96
3 Diff   0.209 0.211       3.14
4 Site_3 0.309 0.302      -2.27
5 Diff   0.309 0.302       6.37
6 Site_4 0.315 0.313      -0.63
7 Diff   0.315 0.313       4.73
8 Site_5 0.31  0.316       1.94
9 Diff   0.31  0.316       2.16

这只是另一种方式:

df %>%
  group_by(Site) %>%
  summarise(Site = "Diff",
            Pre = Pre,
            Post = Post,
            Pct_Change = df$Pct_Change[1] - Pct_Change) %>%
  bind_rows(df) %>%
  arrange(Pre) %>%
  filter(Pct_Change != 0)

# A tibble: 9 x 4
  Site     Pre  Post Pct_Change
  <chr>  <dbl> <dbl>      <dbl>
1 Diff   0.209 0.211       3.14
2 Site_2 0.209 0.211       0.96
3 Site_1 0.244 0.254       4.1 
4 Diff   0.309 0.302       6.37
5 Site_3 0.309 0.302      -2.27
6 Diff   0.31  0.316       2.16
7 Site_5 0.31  0.316       1.94
8 Diff   0.315 0.313       4.73
9 Site_4 0.315 0.313      -0.63

【讨论】:

    【解决方案2】:

    添加“差异”行将偏离 {tidyverse} 工具设计的tidy data 原则。相反,我添加了新列以获得您需要的结果。

    data %>% 
      mutate(pre_diff = .$Pre[1] - Pre,
             post_diff = .$Post[1] - Post)
    
    # # A tibble: 5 x 6
    #   Site     Pre  Post Pct_Change pre_diff post_diff
    #   <chr>  <dbl> <dbl>      <dbl>    <dbl>     <dbl>
    # 1 Site 1 0.244 0.254       4.1     0         0    
    # 2 Site 2 0.209 0.211       0.96    0.035     0.043
    # 3 Site 3 0.309 0.302      -2.27   -0.065    -0.048
    # 4 Site 4 0.315 0.313      -0.63   -0.071    -0.059
    # 5 Site 5 0.31  0.316       1.94   -0.066    -0.062
    

    【讨论】:

      猜你喜欢
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      相关资源
      最近更新 更多