why does map_if() not work within a list

纵饮孤独 提交于 2019-12-03 14:54:24

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!