问题描述
我有以下 df:
library(tidyverse)
df <-
tibble::tribble(
~ city_code,~ name,~ score,489,"Jonh",.45,"Adam",.51,128,"Mary",.33,"Matt",.29,147,"Rob","Gomez",.55,199,"Thomas",.22,"Ryan",.60
)
原始 df 有数千行,每个城市有两个名称。对于每个城市,我想从第二个人中减去第一个人的分数,反之亦然。 df 应如下所示:
df <-
tibble::tribble(
~ city_code,~ score_diff,-.06,.06,.04,-.04,-.1,.1,-.38,.60,.38,)
提前致谢。我也很欣赏有关如何在 R 中学习这些类型函数的任何提示。
解决方法
将正常工作,如果每个 city_code 正好有两个名称
library(tidyverse)
df <-
tibble::tribble(
~ city_code,~ name,~ score,489,"Jonh",.45,"Adam",.51,128,"Mary",.33,"Matt",.29,147,"Rob","Gomez",.55,199,"Thomas",.22,"Ryan",.60
)
df %>% group_by(city_code) %>%
mutate(score_diff = score - lag(score,default = last(score)))
#> # A tibble: 8 x 4
#> # Groups: city_code [4]
#> city_code name score score_diff
#> <dbl> <chr> <dbl> <dbl>
#> 1 489 Jonh 0.45 -0.06
#> 2 489 Adam 0.51 0.06
#> 3 128 Mary 0.33 0.0400
#> 4 128 Matt 0.29 -0.0400
#> 5 147 Rob 0.45 -0.1
#> 6 147 Gomez 0.55 0.1
#> 7 199 Thomas 0.22 -0.38
#> 8 199 Ryan 0.6 0.38
由 reprex package (v2.0.0) 于 2021 年 5 月 25 日创建