为什么map_if()在列表中不起作用

问题描述:

请帮帮我

1)为什么map_if在列表中不起作用

2)有没有办法让它工作

3)如果没有,有什么替代方案

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

提前致谢。

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)


因为您需要映射到一个较低的杠杆,所以列位于2级。所以你可以执行:

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))

或:

map(cyl, map_if, is.numeric, mean)

如果没有,可以

at_depth(cyl, 2, mean)