I'm getting the following error in R when I try to use the lag function (from the dplyr library) on a column of characters in a tibble:
Error in mutate_impl(.data, dots) : Expecting a single string value: [type=logical; extent=1].
This error does not occur for a column of characters in a data frame. I also don't get the error for a column of numbers in either a tibble or a data frame.
Does anyone know why I'm getting this discrepancy in the lag function for data frames versus tibbles? Thanks!
Here is some sample code that reproduces the error. I have examples of both when lag works and when it doesn't. I have tried updating the tidyverse and dplyr libraries on my machine but I'm still getting the same error.
tib = data_frame(x = c('a','b','c'), y = 1:3)
# lagging column of characters in tibble throws error
res = tib %>%
mutate(lag_n = lag(x, n=1, default = NA))
# lagging column of numbers in tibble does NOT throw error
res = tib %>%
mutate(lag_c = lag(y, n=1, default = NA))
df = data.frame(x = c('a','b','c'), y = 1:3)
# lagging column of characters in data frame does NOT throw error
res = df %>%
mutate(lag_n = lag(x, n=1, default = NA))
# lagging column of numbers in data frame does NOT throw error
res = df %>%
mutate(lag_c = lag(y, n=1, default = NA))
You're running into this error because dplyr
and tibble
are strict about the type of NA
values that they allow you to use (or, more specifically, they are more strict about checking the type of the variable you create). You needed NA_character_
, like so:
res = tib %>%
mutate(lag_n = lag(x, n=1, default = NA_character_))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With