Apply a list to a function that outputs a dataframe

泪湿孤枕 提交于 2019-12-06 06:38:31

To stay in tidyverse, maybe something like this:

c("mpg>15", "am==1") %>% map(myfun) %>% bind_rows

But, as @alistaire points out in a comment, you can shorten this by using map_df, which returns a data frame:

c("mpg>15", "am==1") %>% map_df(myfun)

A mixed option, three equivalent ways, using lapply:

lapply(c("mpg>15", "am==1"), myfun) %>% bind_rows 
c("mpg>15", "am==1") %>% lapply(myfun) %>% bind_rows
bind_rows(lapply(c("mpg>15", "am==1"), myfun))

Or, to get a little more perverse about mixing base and tidyverse:

c("mpg>15", "am==1") %>% lapply(myfun) %>% do.call(rbind, .)

And for base R traditionalists:

do.call(rbind, lapply(c("mpg>15", "am==1"), myfun))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!