使用 group_by 平滑分组数据和 R 中 stats 包的平滑函数

Smoothing grouped data using group_by and smooth function from stats package in R

我正在尝试平滑 tibble 对象中的某些列。我正在使用类似于下面的数据。

rep <- tibble(source = rep(1:5, each = 921),
              V1 = rnorm(4605, mean = 3, sd = 0.2),
              V2 = rnorm(4605, mean = 3, sd = 0.2),
              V3 = rnorm(4605, mean = 3, sd = 0.2),
              V4 = rnorm(4605, mean = 3, sd = 0.2),
              V5 = rnorm(4605, mean = 3, sd = 0.2)) %>% 
        group_by(source) %>% 
        mutate_at(vars(V1:V5), smooth, kind = "3RS3R")

问题是:每次我 运行 代码时,R 都会给我以下错误消息:

Error: Problem with `mutate()` input `V1`.
x Input `V1` must return compatible vectors across groups
i Input `V1` is `(function (x, kind = c("3RS3R", "3RSS", "3RSR", "3R", "3", "S"), ...`.
i Result type for group 1 (source = 1): <tukeysmooth>.
i Result type for group 3 (source = 3): <tukeysmooth>.
Run `rlang::last_error()` to see where the error occurred.

根据错误消息,我的代码正在返回不兼容的向量 across 组,但在消息的末尾,pos可以看到它返回相同类型 os 向量 (<tukeysmooth>)。如何在 those 五列中使用平滑的 across 这个分组数据?我尝试了不同的 dplyr 动词和 tibble 格式,它们总是给我这个错误。

谢谢!

将输出从 smooth 转换为数字。

library(dplyr)

set.seed(123)
tibble(source = rep(1:5, each = 921),
       V1 = rnorm(4605, mean = 3, sd = 0.2),
       V2 = rnorm(4605, mean = 3, sd = 0.2),
       V3 = rnorm(4605, mean = 3, sd = 0.2),
       V4 = rnorm(4605, mean = 3, sd = 0.2),
       V5 = rnorm(4605, mean = 3, sd = 0.2)) %>% 
  group_by(source) %>% 
  mutate(across(V1:V5, ~as.numeric(smooth(., kind = "3RS3R")))) %>%
  ungroup

#   source    V1    V2    V3    V4    V5
#    <int> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1      1  2.89  3.04  2.91  3.14  2.88
# 2      1  2.95  3.06  2.91  3.14  2.91
# 3      1  3.01  3.07  3.05  3.08  3.10
# 4      1  3.03  3.07  3.05  3.07  3.11
# 5      1  3.03  2.98  3.05  3.07  3.11
# 6      1  3.03  2.84  3.05  3.00  3.04
# 7      1  3.03  2.77  2.89  2.93  2.77
# 8      1  2.91  2.77  2.70  2.93  2.70
# 9      1  2.91  2.91  2.70  2.93  2.70
#10      1  2.91  3.08  2.71  2.93  2.70
# … with 4,595 more rows