D Kincaid
D Kincaid

Reputation: 281

How do I use approx() inside mutate_at() with a conditional statement in dplyr?

I want to interpolate missing values using dplyr, piping, and approx().

Data:

test <- structure(list(site = structure(c(3L, 3L, 3L, 3L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L), .Label = c("lake", "stream", "wetland"), class = "factor"), 
    depth = c(0L, -3L, -4L, -8L, 0L, -1L, -3L, -5L, 0L, -2L, 
    -4L, -6L), var1 = c(1L, NA, 3L, 4L, 1L, 2L, NA, 4L, 1L, NA, 
    NA, 4L), var2 = c(1L, NA, 3L, 4L, NA, NA, NA, NA, NA, 2L, 
    NA, NA)), .Names = c("site", "depth", "var1", "var2"), class = "data.frame", row.names = c(NA, 
-12L))

This code works:

library(tidyverse)

# interpolate missing var1 values for each site using approx()
test_int <- test %>% 
  group_by(site) %>% 
  mutate_at(vars(c(var1)),
            funs("i" = approx(depth, ., depth, rule=1, method="linear")[["y"]]))

But the code no longer works if it encounters a grouping (site & var) that doesn't have at least 2 non-NA values, e.g.,

# here I'm trying to interpolate missing values for var1 & var2
test_int2 <- test %>% 
  group_by(site) %>% 
  mutate_at(vars(c(var1, var2)),
            funs("i" = approx(depth, ., depth, rule=1, method="linear")[["y"]]))

R appropriately throws this error: Error in mutate_impl(.data, dots) : Evaluation error: need at least two non-NA values to interpolate.

How do I include a conditional statement or filter so that it only tries to interpolate cases where the site has at least 2 non-NA values and skips the rest or returns NA for those?

Upvotes: 2

Views: 378

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18425

This will do what you are looking for...

test_int2 <- test %>% 
             group_by(site) %>% 
             mutate_at(vars(c(var1, var2)),
                       funs("i"=if(sum(!is.na(.))>1) 
                                  approx(depth, ., depth, rule=1, method="linear")[["y"]] 
                                else 
                                  NA))

test_int2
# A tibble: 12 x 6
# Groups:   site [3]
      site depth  var1  var2 var1_i var2_i
    <fctr> <int> <int> <int>  <dbl>  <dbl>
 1 wetland     0     1     1    1.0    1.0
 2 wetland    -3    NA    NA    2.5    2.5
 3 wetland    -4     3     3    3.0    3.0
 4 wetland    -8     4     4    4.0    4.0
 5    lake     0     1    NA    1.0     NA
 6    lake    -1     2    NA    2.0     NA
 7    lake    -3    NA    NA    3.0     NA
 8    lake    -5     4    NA    4.0     NA
 9  stream     0     1    NA    1.0     NA
10  stream    -2    NA     2    2.0     NA
11  stream    -4    NA    NA    3.0     NA
12  stream    -6     4    NA    4.0     NA

Upvotes: 1

Related Questions