简体   繁体   中英

Create function in R to apply to multiple datasets

I have this code, recommended from a Stackoverflow user that works very well. I have several datasets that I wish to apply this code to. Would I have to continuously apply each dataset to the code, or is there something else that I can do? (Like store it in some sort of function?)

I have datsets

df1, df2, df3, df4. I do not wish to rbind these datasets.

Dput for each dataset:

structure(list(Date = structure(1:6, .Label = c("1/2/2020 5:00:00 PM", 
"1/2/2020 5:30:01 PM", "1/2/2020 6:00:00 PM", "1/5/2020 7:00:01 AM", 
"1/6/2020 8:00:00 AM", "1/6/2020 9:00:00 AM"), class = "factor"), 
Duration = c(20L, 30L, 10L, 5L, 2L, 8L)), class = "data.frame", row.names = c(NA, 
-6L))

CODE:

df %>%
group_by(Date = as.Date(dmy_hms(Date))) %>% 
summarise(Total_Duration = sum(Duration), Count = n())

This is what I have been doing for each:(etc)

df1 %>%
group_by(Date = as.Date(dmy_hms(Date))) %>% 
summarise(Total_Duration = sum(Duration), Count = n())


df2 %>%
group_by(Date = as.Date(dmy_hms(Date))) %>% 
summarise(Total_Duration = sum(Duration), Count = n())


df3 %>%
group_by(Date = as.Date(dmy_hms(Date))) %>% 
summarise(Total_Duration = sum(Duration), Count = n())

Is there a way to:

 Store_code<-
 df %>%
 group_by(Date = as.Date(dmy_hms(Date))) %>% 
 summarise(Total_Duration = sum(Duration), Count = n())

and then apply each dataset easily to this code?

df1(Store_code)
df2(Store_code)

Any suggestion is appreciated.

We can use mget to return all the objects into a list , use map to loop over the list and apply the function

library(dplyr)
library(lubridate)
library(purrr)
f1 <- function(dat) {
      dat %>%
        group_by(Date = as.Date(dmy_hms(Date))) %>% 
         summarise(Total_Duration = sum(Duration), Count = n())
      }

lst1 <- map(mget(ls(pattern = "^df\\d+$")), f1)

Here, we assume the column names are the same ie 'Date', 'Duration' in all the datasets. If it is a different one, then can pass as another argument to function

f2 <- function(dat, datecol, durationcol) {
      dat %>%
        group_by(Date = as.Date(dmy_hms({{datecol}}))) %>% 
         summarise(Total_Duration = sum({{durationcol}}), Count = n())
      }

and apply the function as

f2(df1, Date, Duration)

Or in the loop

lst1 <- map(mget(ls(pattern = "^df\\d+$")), f2, 
         datecol = Date, durationcol = Duration)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM