Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: dplyr::lag throws error when trying to lag characters in tibble

Tags:

r

dplyr

lag

tibble

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))
like image 765
Kimberly Brink Avatar asked Aug 17 '18 20:08

Kimberly Brink


1 Answers

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_))
like image 119
Jake Fisher Avatar answered Nov 16 '22 08:11

Jake Fisher