Juliana
Juliana

Reputation: 103

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

I am trying to smooth some columns in a tibble object. I am using a data similiar to the one bellow.

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")

The problem is: everytime I run the code, R gives me the followin error message:

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.

According to the error message, my code is returning incompatible vectors across groups, but at the end of the message it's possible to see that it's returning the same type os vectors (<tukeysmooth>). How can I use smooth across this grouped data in those five columns? I have tried different dplyr verbs and formats of tibble, all of them always give me this error.

Thank you!

Upvotes: 3

Views: 771

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389145

Convert output from smooth to numeric.

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

Upvotes: 4

Related Questions