Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does map_if() not work within a list

Please help me

1) Why does map_if not work within a list
2) Is there a way to make it work
3) If not, what are the alternatives

Thanks in advance.

library(dplyr) 
library(purrr) 

cyl <- split(mtcars, mtcars$cyl) 

# This works
map_if(mtcars, is.numeric, mean) 

# This does not work 
map_if(cyl, is.numeric, mean)
like image 916
cephalopod Avatar asked Mar 14 '17 09:03

cephalopod


1 Answers

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)
like image 85
Axeman Avatar answered Sep 17 '22 08:09

Axeman