Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translation of lead/lag function to data.table changes class of column #469

Closed
alia-rahim opened this issue Feb 22, 2024 · 2 comments
Closed

Comments

@alia-rahim
Copy link

library(dtplyr)
library(dplyr, warn.conflicts = FALSE)

data <- data.frame(x = c("A", "A", "B", "B"), y = c(1,2,3,4))

df <- lazy_dt(data) %>%
group_by(x) %>%
transmute(lead(y,1)) %>%
ungroup() %>%
collect()

class(df$lead(y, 1))

@alia-rahim
Copy link
Author

If you group by and use the lead/lag functions the resulting column becomes a list instead of the input datatype

library(dtplyr)
library(dplyr, warn.conflicts = FALSE)

data <- data.frame(x = c("A", "A", "B", "B"), y = c(1,2,3,4))

df <- lazy_dt(data) %>%
  group_by(x) %>%
  transmute(lead(y,1)) %>%
  ungroup() %>%
  collect()


df2 <- data %>%
  group_by(x) %>%
  transmute(lead(y,1)) %>%
  ungroup()

class(df$`lead(y, 1)`)
#> [1] "list"
class(df2$`lead(y, 1)`)
#> [1] "numeric"

Created on 2024-02-22 with reprex v2.1.0

@markfairbanks
Copy link
Collaborator

This is actually a data.table issue that has been fixed in their latest development version Rdatatable/data.table#5939

If you update your data.table to the dev version using data.table::update_dev_pkg() it will work correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants