Michael Lachanski
2022-Dec-31 06:50 UTC
[R] Functional Programming Problem Using purr and R's data.table shift function
Hello, I am trying to make a habit of "functionalizing" all of my code as recommended by Hadley Wickham. I have found it surprisingly difficult to do so because several intermediate features from data.table break or give unexpected results using purrr and its data.table adaptation, tidytable. Here is the a minimal working example of what has stumped me most recently: == library(data.table); library(tidytable) minimal_failing_function <- function(A){ DT <- data.table(A) DT[ , A:= shift(A, fill = NA, type = "lag", n = 1)] %>% `[` return(DT)} # works minimal_failing_function(c(1,2)) # fails tidytable::pmap_dfr(.l = list(c(1,2)), .f = minimal_failing_function) ==These should ideally give the same output, but do not. This also fails using purrr::pmap_dfr rather than tidytable. I am using R 4.2.2 and I am on Mac OS Ventura 13.1. Thank you for any help you can provide or general guidance. =Michael Lachanski PhD Student in Demography and Sociology MA Candidate in Statistics University of Pennsylvania mikelach at sas.upenn.edu [[alternative HTML version deleted]]
Rui Barradas
2022-Dec-31 12:39 UTC
[R] Functional Programming Problem Using purr and R's data.table shift function
?s 06:50 de 31/12/2022, Michael Lachanski escreveu:> Hello, > > I am trying to make a habit of "functionalizing" all of my code as > recommended by Hadley Wickham. I have found it surprisingly difficult to do > so because several intermediate features from data.table break or give > unexpected results using purrr and its data.table adaptation, tidytable. > Here is the a minimal working example of what has stumped me most recently: > > ==> > library(data.table); library(tidytable) > > minimal_failing_function <- function(A){ > DT <- data.table(A) > DT[ , A:= shift(A, fill = NA, type = "lag", n = 1)] %>% `[` > return(DT)} > # works > minimal_failing_function(c(1,2)) > # fails > tidytable::pmap_dfr(.l = list(c(1,2)), > .f = minimal_failing_function) > > > ==> These should ideally give the same output, but do not. This also fails > using purrr::pmap_dfr rather than tidytable. I am using R 4.2.2 and I am on > Mac OS Ventura 13.1. > > Thank you for any help you can provide or general guidance. > > > => Michael Lachanski > PhD Student in Demography and Sociology > MA Candidate in Statistics > University of Pennsylvania > mikelach at sas.upenn.edu > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, Use map_dfr instead of pmap_dfr. library(data.table) library(tidytable) minimal_failing_function <- function(A) { DT <- data.table(A) DT[ , A:= shift(A, fill = NA, type = "lag", n = 1)] %>% `[` return(DT) } # works tidytable::map_dfr(.x = list(c(1,2)), .f = minimal_failing_function) #> # A tidytable: 2 ? 1 #> A #> <dbl> #> 1 NA #> 2 1 Hope this helps, Rui Barradas