How to apply list to a function which give data frame as output

0 votes

I have applied a single argument function which outputs a dataframe.

library(tidyverse) 
my_function <-function(x) {mtcars %>%
                         filter_(x) %>%
                         group_by(cyl) %>%
                         summarise(mean(disp), mean(drat)) %>%
                         mutate(group=x)}

When I feed a single argument into this function, it gives the output as expected i.e. a dataframe

   my_function('mpg>15')

   cyl      mean(disp)   mean(drat)     group
    4       105           4.07          mpg>15
    6       183           3.59          mpg>15
    8       105           3.20          mpg>15

Now, how do I apply such a function to a list of arguments that gives output as one dataframe which combines all the reults over the list.

For example I would like to apply my_function to a list

c('mpg>15', 'drat>4.2')

and, as the result, to obtain a single dataframe:

cyl      mean(disp)   mean(drat)     group
 4       105           4.07          mpg>15
 6       183           3.59          mpg>15
 8       105           3.20          mpg>15
 4       89            4.53          drat>4.2
 8       351           4.22          drat>4.2

How to do that ?

Apr 11, 2018 in Data Analytics by nirvana
• 3,040 points
17 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

If you use  tidyverse, you can use something like this:

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

But, we can also shorten this by using map_df, which returns a data frame:

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

A mixed option, three equivalent ways, using lapply:

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

You can also mix base and tidyverse:

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

Another option is as follows:

do.call(rbind, lapply(c("mpg>15", "am==1"), my_function))
answered Apr 11, 2018 by darklord
• 6,140 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,700 points
31 views
0 votes
1 answer

How to convert a list to data frame in R?

Let's assume your list of lists is ...READ MORE

answered Apr 12, 2018 in Data Analytics by nirvana
• 3,040 points

edited Apr 12, 2018 by nirvana 1,302 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
32 views
0 votes
1 answer
0 votes
1 answer

How to use dplyr functions such as filter() inside nested data frames with map()

You can use map() call as follows:  map(full, ...READ MORE

answered Apr 6, 2018 in Data Analytics by darklord
• 6,140 points
87 views
0 votes
1 answer
0 votes
1 answer

How to join a list of data frames using map()

You can use reduce set.seed(24) r1 <- map(c(5, 10, ...READ MORE

answered Apr 11, 2018 in Data Analytics by kappa3010
• 2,010 points
20 views
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
52 views
0 votes
1 answer

How to remove rows with missing values (NAs) in a data frame?

You can use complete.cases in the following ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
2,957 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.