[R] Functional Programming Problem Using purr and R's data.table shift function

Michael Lachanski m|ke|@ch @end|ng |rom @@@@upenn@edu
Sat Dec 31 07:50:50 CET 2022


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 using sas.upenn.edu

	[[alternative HTML version deleted]]



More information about the R-help mailing list