R 使用 dplyr 改变新行

R mutate new row using dplyr

我有以下数据:

library(tidyverse)

d1 <- data_frame('Type' = "all types",
             `1991` = c(1200),
             `1992` = c(4000),
             `1993` = c(6000),
             `1994` = c(8000))

我想创建一个新行,这是对每年的计算。计算值为:-10%。因此,数据将如下所示:

d2 <- data_frame('Type' = "all types", "summary",
             `1991` = c(1200, 1080),
             `1992` = c(4000, 3600),
             `1993` = c(6000, 5400),
             `1994` = c(8000, 7200))

现在,有一个新行,即值负 10%。如果数据是另一种形状,那么我可以使用 mutate 轻松地做到这一点。但是,我想保留短格式。

谢谢

library(tidyverse)

d1 <- data_frame('Type' = "all types",
                 `1991` = c(1200),
                 `1992` = c(4000),
                 `1993` = c(6000),
                 `1994` = c(8000))

d1 %>%
  mutate_if(is.numeric, ~.*0.9) %>%
  rbind(d1) %>%
  mutate(summary = "summary")

# # A tibble: 2 x 6
#   Type      `1991` `1992` `1993` `1994` summary
#   <chr>      <dbl>  <dbl>  <dbl>  <dbl> <chr>  
# 1 all types   1080   3600   5400   7200 summary
# 2 all types   1200   4000   6000   8000 summary