Using a list of data tables with map

Hi there,
I have a quick question regarding using map with a list of data tables. I have tried two approaches which seem equivalent but the first throws an error:

DTs <- list(DT1, DT2, DT3)
result <- DTs %>% map(.[, .N, by = id]) #this does not work and gives the error below

Error in as_mapper(.f, ...) : object 'id' not found

result <- DTs %>% map(function(x) x[, .N, by = id]) #this works

I'm a little confused as to why these approaches are different?

Will

Hi @WillP,

interestingly with your first version result <- DTs %>% map(.[, .N, by = id]) I get a different error: Error in .[, .N, by = id] : incorrect number of dimensions, but since I don't know how your DTs look like, it might not be the same error that we get. I have simply generated them as

DT1 <- data.table(id = 1, value = runif(10))
DT2 <- data.table(id = 2, value = runif(10))
DT3 <- data.table(id = 3, value = runif(10))

This however, works:

result <- DTs %>% map(~.[, .N, by = id])

To throw some light on the functioning of purrr::map I tried the following versions:

1:10 %>% map(. + 3)
1:10 %>% map(~. + 3)
1:10 %>% map(~.x + 3)

Number 1 returns a list of NULLs (no error though), both 2 and 3 work as expected and return a list with numbers from 4 through 13. So, reading the documentation of map::purrr carefully it is said that for single argument functions you can use . but with the formula notation ~, so you need to say ~. and not just ., at least that is how I am reading it.

3 Likes

Ah right! I thought it might be something fairly simple, but I did assume that I could just use a .

Will

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.