1
votes

Example data frame:

df <- tibble(x = c('A', 'B', 'C'),
             y = c('D', 'E', 'F'),
             z = c(1, NA, 1))

I want to use the tidyr::fill funtion to replace the NA value in vector z. I can do it like this:

df %>% 
  fill(z, .direction = 'down')
# A tibble: 3 x 3
  x     y         z
  <chr> <chr> <dbl>
1 A     D         1
2 B     E         1
3 C     F         1

What I want to try now is to create a new variable while applying the fill() funtion to vector z (so that I keep the original vector z with the NA). I tried this:

df %>% 
  mutate(z2 = fill(z, .direction = 'down'))

But I only get the following error message:

no applicable method for 'mutate_' applied to an object of class "c('double', 'numeric')"

Why can't I use the tidyr::fill inside the mutate function? What I've learned so far (and I'm relatively new to R), is that I can use other functions inside mutate, such as dplyr::recode, case_when, fct_reorder etc. in order to change an existing vector but then relocate the change to a new variable.

The end result should look like this:

# A tibble: 3 x 4
  x     y         z    z2
  <chr> <chr> <dbl> <dbl>
1 A     D         1     1
2 B     E        NA     1
3 C     F         1     1
1

1 Answers

2
votes

It may be more easier with na.locf from zoo which expects a vector/column as input instead of a tibble

library(dplyr)
library(zoo)
df %>% 
    mutate(z2 = na.locf0(z))

-output

# A tibble: 3 x 4
#  x     y         z    z2
#  <chr> <chr> <dbl> <dbl>
#1 A     D         1     1
#2 B     E        NA     1
#3 C     F         1     1

Or if we want to use fill, create a duplicate column with new name and then apply fill on it because the Usage of fill is

fill(data, ..., .direction = c("down", "up", "downup", "updown"))

and

data - A data frame.

library(tidyr)
df %>% 
    mutate(z2 = z) %>% 
    fill(z2, .direction = 'down')

Or we can use cur_data

df %>%
    mutate(z2 = fill(cur_data(), z, .direction = 'down')$z)

-output

# A tibble: 3 x 4
#  x     y         z    z2
#  <chr> <chr> <dbl> <dbl>
#1 A     D         1     1
#2 B     E        NA     1
#3 C     F         1     1