简体   繁体   中英

How do you use approx() inside of mutate_at()?

I'm having issues getting approx() to work inside of a mutate_at(). I did manage to get what I want using a very long mutate() function, but for future reference I was wondering if there was a more graceful and less copy-pasting mutate_at() way to do this.

The overarching problem is merging a dataset with data from 1 year intervals to one with 3 year intervals, and interpolating years with no data in the dataset with 3 year intervals. There are missing values in between the years, and one year that requires some form of extrapolation.

library("tidyverse")

demodf <- data.frame(groupvar = letters[rep(1:15, each = 6)],
                     timevar = c(2000, 2003, 2006, 2009, 2012, 2015),
                     x1 = runif(n = 90, min = 0, max = 3),
                     x2 = runif(n = 90, min = -1, max = 4),
                     x3 = runif(n = 90, min = 1, max = 12),
                     x4 = runif(n = 90, min = 0, max = 30),
                     x5 = runif(n = 90, min = -2, max = 5),
                     x6 = runif(n = 90, min = 20, max = 50),
                     x7 = runif(n = 90, min = 1, max = 37),
                     x8 = runif(n = 90, min = 0.3, max = 0.5))

demotbl <- tbl_df(demodf)

masterdf <- data.frame(groupvar = letters[rep(1:15, each = 17)],
                      timevar = 2000:2016,
                      z1 = runif(n = 255, min = 0, max = 1E6))

mastertbl <- tbl_df(masterdf)

joineddemotbls <- mastertbl %>% left_join(demotbl, by = c("groupvar", "timevar"))

View(joineddemotbls)

joineddemotblswithinterpolation <- joineddemotbls %>% group_by(groupvar) %>%
  mutate(x1i = approx(timevar, x1, timevar, rule = 2, f = 0, ties = mean, method = "linear")[["y"]],
         x2i = approx(timevar, x2, timevar, rule = 2, f = 0, ties = mean, method = "linear")[["y"]],
         x3i = approx(timevar, x3, timevar, rule = 2, f = 0, ties = mean, method = "linear")[["y"]],
         x4i = approx(timevar, x4, timevar, rule = 2, f = 0, ties = mean, method = "linear")[["y"]],
         x5i = approx(timevar, x5, timevar, rule = 2, f = 0, ties = mean, method = "linear")[["y"]],
         x6i = approx(timevar, x6, timevar, rule = 2, f = 0, ties = mean, method = "linear")[["y"]],
         x7i = approx(timevar, x7, timevar, rule = 2, f = 0, ties = mean, method = "linear")[["y"]],
         x8i = approx(timevar, x8, timevar, rule = 2, f = 0, ties = mean, method = "linear")[["y"]])

View(joineddemotblswithinterpolation)

# this is what I want

That works pretty well. But I've tried all these mutate_at() variants and have not gotten them to work. I am sure there is an error in the syntax somewhere...

joineddemotblswithinterpolation2 <- joineddemotblswithinterpolation %>% group_by(groupvar) %>%
  mutate_at(vars(x1, x2, x3, x4, x5, x6, x7, x8), approx(timevar, ., timevar, rule = 2, f = 0, ties = mean, method = "linear")[["y"]])

# error

joineddemotblswithinterpolation2 <- joineddemotblswithinterpolation %>% group_by(groupvar) %>%
  mutate_at(vars(x1, x2, x3, x4, x5, x6, x7, x8), approxfun(timevar, ., timevar, rule = 2, f = 0, ties = mean, method = "linear")[["y"]])

# error

joineddemotblswithinterpolation2 <- joineddemotblswithinterpolation %>% group_by(groupvar) %>%
  mutate_at(vars(x1, x2, x3, x4, x5, x6, x7, x8), funs(approxfun(timevar, ., timevar, rule = 2, f = 0, ties = mean, method = "linear")[["y"]]))

# error

joineddemotblswithinterpolation2 <- joineddemotblswithinterpolation %>% group_by(groupvar) %>%
  mutate_at(vars(x1, x2, x3, x4, x5, x6, x7, x8), funs(approxfun(timevar, ., rule = 2, f = 0, ties = mean, method = "linear")[["y"]]))

I even tried na.approx(), but also to no avail...

library("zoo")
joineddemotblswithinterpolation2 <- joineddemotblswithinterpolation %>% group_by(groupvar) %>%
  mutate_at(vars(x1, x2, x3, x4, x5, x6, x7, x8), na.approx(., timevar, na.rm = FALSE))

I've kind of constructed these different trials from the following related questions:

Using approx in dplyr

Linear Interpolation using dplyr

Using approx() with groups in dplyr

linear interpolation with dplyr but skipping groups with all missing values

R: Interpolation of NAs by group

Thanks for any help!

You're very close. This works for me:

joineddemotblswithinterpolation <- joineddemotbls %>%
  group_by(groupvar) %>%
  mutate_at(vars(starts_with("x")), # easier than listing each column separately
            funs("i" = approx(timevar, ., timevar, rule = 2, f = 0, ties = mean,
                              method = "linear")[["y"]]))

This will create columns x1_i , x2_i etc. with the interpolated values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM