why does map_if() not work within a list
Because you need to map to one lever lower, the columns are at level 2. So you can do:
map(cyl, ~map_if(., is.numeric, mean))
Or:
map(cyl, map_if, is.numeric, mean)
Without the if one could do
map_depth(cyl, 2, mean)
You can try lapply
:
lapply(cyl, function(x) map_if(x, is.numeric, mean))
You are attempting to use map_if()
over a list
of data.frames
. The predicate will be tested against each data.frame
, rather than each column of the data.frame e.g.
is.numeric( cyl[[1]] )
# [1] FALSE
And that is because...
is.data.frame( cyl[[1]] )
# [1] TRUE