我有一个这样的df:
structure(list(Date = structure(c(18605, 18604, 18598, 18597,
18590, 18584, 18583, 18578, 18570, 18569, 18563, 18562, 18557,
18549, 18548, 18542, 18541, 18536, 18534, 18529, 18521, 18520,
18515, 18508, 18500, 18499, 18493, 18492, 18486, 18485, 18479,
18478, 18472, 18471, 18465, 18464, 18458, 18457, 18450, 18445,
18444, 18437, 18436, 18430, 18429, 18424, 18416, 18415, 18410,
18409, 18403, 18402, 18396, 18388, 18387, 18381, 18380, 18374,
18373, 18368, 18367, 18360, 18359, 18354, 18340, 18338, 18331,
18325, 18317, 18312, 18289, 18282, 18275, 18268), class = "Date"),
`Type 1` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 0.3, NA, NA, NA, NA, 0.4, NA, NA,
NA, NA, 0.2, NA, NA, NA, NA, 0.7, NA, NA, NA, NA, NA, 0.5,
NA, NA, NA, NA, 0.3, NA, NA, NA, NA, NA, 0.4, NA, NA, NA,
0.3, NA, NA, NA, NA, NA, NA, NA, NA, 0.6, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), `Type 2` = c(NA, NA, 0.1, NA,
NA, 0.1, NA, 0.2, NA, 0.2, 0.1, NA, 0.2, 0.2, NA, 0.1, NA,
NA, 0.1, NA, 0.2, NA, NA, 0.4, 0.2, NA, 0.3, NA, 0.2, NA,
0.3, NA, 0.6, NA, 0.4, NA, NA, 0.2, NA, 0.4, 0.6, NA, 0.3,
NA, 0.2, 0.7, NA, 0.1, 0.3, NA, 0.2, NA, NA, NA, 0.3, NA,
0.1, 0.3, NA, NA, 0.3, 0.2, NA, NA, NA, NA, 0.6, NA, 0.4,
NA, 0.2, NA, NA, 0.2), `Type 3` = c(NA, 0.3, NA, 0.3, 0.4,
NA, 0.2, NA, 0.3, NA, NA, 0.2, NA, NA, 0.2, NA, 0.2, NA,
NA, 0.1, NA, 0.2, NA, NA, NA, 0.3, NA, NA, NA, 0.4, NA, 0.3,
NA, 0.7, NA, 0.2, 0.5, 0.4, NA, 0.4, NA, 0.8, 0.4, NA, 0.2,
0.6, 0.3, 0.2, NA, NA, NA, 0.4, 0.4, NA, 0.2, 0.3, NA, 0.2,
0.3, 0.4, NA, 0.7, NA, NA, 1.4, NA, NA, 1.4, NA, 1, NA, NA,
0.3, NA), `Type 4` = c(NA, 0.4, NA, 0.1, 0.1, NA, 0.1, NA,
NA, 0.1, NA, 0.1, 0.2, NA, 0.2, NA, 0.2, 0.3, NA, NA, NA,
0.2, 0.3, 0.3, NA, NA, NA, 0.5, NA, 0.6, NA, 0.7, NA, NA,
NA, 1.2, 1, NA, 0.3, NA, 1.1, NA, NA, 0.4, NA, NA, NA, NA,
0.2, 0.2, NA, NA, 0.2, NA, NA, 0.1, NA, NA, NA, 0.2, 0.3,
NA, 0.2, 0.3, NA, 1.8, NA, NA, NA, NA, NA, 0.2, NA, NA)), row.names = c(NA,
-74L), class = c("tbl_df", "tbl", "data.frame"))
我希望能够计算出与“类型”平均值的偏差,并显示它而不是原始数据。例如,2020 年 10 月 1 日,“类型 1”的 df 当前显示为 0.3。相反,我希望它显示整个数据集与“类型 1”平均值的偏差。
这可能吗?
library(tidyverse)
df %>%
mutate(across(2:5, ~ mean(., na.rm = T) - .x ))
# A tibble: 74 × 5
Date `Type 1` `Type 2` `Type 3` `Type 4`
<date> <dbl> <dbl> <dbl> <dbl>
1 2020-12-09 NA NA NA NA
2 2020-12-08 NA NA 0.119 -0.00968
3 2020-12-02 NA 0.171 NA NA
4 2020-12-01 NA NA 0.119 0.290
5 2020-11-24 NA NA 0.0194 0.290
6 2020-11-18 NA 0.171 NA NA
7 2020-11-17 NA NA 0.219 0.290
8 2020-11-12 NA 0.0714 NA NA
9 2020-11-04 NA NA 0.119 NA
10 2020-11-03 NA 0.0714 NA 0.290
# … with 64 more rows
# ℹ Use `print(n = ...)` to see more rows