在Tidyverse中按变异除以最后一行

时间:2019-07-30 18:16:21

标签: r dplyr tidyverse mutate

这是一个相对简单的问题,我有一个如下的数据集

df <- structure(list(term = c("(Intercept)", "overall_quality", "overall_costs", 
"wwpf"), estimate = c(0.388607224137536, 0.456477162621961, 0.485612564501229, 
NA), std.error = c(0.499812263278414, 0.0987819420575201, 0.108042289289401, 
NA), statistic = c(0.777506381273137, 4.62105879995918, 4.49465267438447, 
NA), p.value = c(0.440597919486169, 0.0000279867005591494, 0.0000426773877613654, 
NA), average = c(NA, 8.09615384615385, 7.86538461538461, 7.90384615384615
), Elasticity = c(NA, 3.69570933584318, 3.81952959386543, NA)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -4L))

我正尝试在下面使用

df %>%  mutate(Elasticity= average*estimate/average[nrow(df)])

预期输出:https://ibb.co/42ptLXx

基本上,除以最后一行的值,由于我试图将其合并到函数中,因此我需要方法是动态的而不是硬编码的值。 请帮忙!

1 个答案:

答案 0 :(得分:3)

我们可以使用n()返回最后一行的索引,以设置该列的值

library(dplyr)
df %>% 
    mutate(Elasticity= average*estimate/average[n()]) 

如果我们需要一个函数(使用rlang_0.4.0),可以使用{{..}}进行评估

f1 <- function(dat, col1, col2) {
        dat %>%
           mutate(Elasticity = {{col1}} * {{col2}}/{{col1}}[n()])
 }

f1(df, average, estimate)
# A tibble: 4 x 7
#  term            estimate std.error statistic    p.value average Elasticity
#  <chr>              <dbl>     <dbl>     <dbl>      <dbl>   <dbl>      <dbl>
#1 (Intercept)        0.389    0.500      0.778  0.441       NA        NA    
#2 overall_quality    0.456    0.0988     4.62   0.0000280    8.10      0.468
#3 overall_costs      0.486    0.108      4.49   0.0000427    7.87      0.483
#4 wwpf              NA       NA         NA     NA            7.90     NA