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,130 points
443 views

1 answer to this question.

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 Sahiti
• 6,370 points

Related Questions In Data Analytics

0 votes
1 answer
+1 vote
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,720 points
1,229 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,130 points

edited Apr 12, 2018 by nirvana 21,818 views
0 votes
2 answers

how to convert a data frame into a list in R

Convert whole data frame into a list?? ...READ MORE

answered Sep 4, 2019 in Data Analytics by anonymous
• 33,030 points
930 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 Sahiti
• 6,370 points
4,259 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,090 points
1,223 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,401 views
0 votes
2 answers

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

Hi, The below code returns rows without ...READ MORE

answered Aug 20, 2019 in Data Analytics by anonymous
• 33,030 points
14,385 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP