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