Adjust result generated when I call a function

I made a function that works properly. However, I'm having trouble calling the function. See that when I do this f2(df1,"2","Sunday","EFG",""), comes the table with all the information, however, I would only like the fourth line of meanTime, which refers up to Id=2, Week=Sunday, Category=EFG and DT=""

library(dplyr)
library(tidyr)
library(lubridate)


df1<- structure(
  list(
    Id = c(1, 1, 1, 1, 2, 2, 2, 2),
   date1 = c("2022-01-06","2022-01-06","2022-01-06","2022-01-06","2022-01-06",
                        "2022-01-06","2022-01-06","2022-01-06"),
    date2 = c("2022-01-02","2022-01-03","2022-01-09","2022-01-10","2022-01-02",
                        "2022-01-03","2022-01-09","2022-01-10"),
   Week = c("Sunday","Monday","Sunday","Monday","Sunday","Monday","Sunday","Monday"),
   Category = c("EFG", "ABC","EFG","ABC","EFG", "ABC","EFG","ABC"),
    DT= c("", "","","","", "","",""),
   Time = c(7.13, 1.98, 5.63, 4.46,5.23, 2.33, 5.34, 4.36)),
  row.names = c(NA, 8L), class = "data.frame")

f2 <- function(df1,Id,Week, Category,DT) {
  
  nms <- c('Time|time')
  
  meanTime <- df1 %>%
    group_by(Id,Week = tools::toTitleCase(Week), Category,DT) %>% 
    summarise(across(matches(nms), mean, .names = 'Time',na.rm = TRUE), .groups = 'drop')
  
  meanTime <- transform(meanTime, Time = format(round(Time, digits = 2), nsmall = 2))           
  
  return(meanTime)
}

f2(df1,"2","Sunday","EFG","")

  Id   Week Category DT Time
1  1 Monday      ABC    3.22
2  1 Sunday      EFG    6.38
3  2 Monday      ABC    3.35
4  2 Sunday      EFG    5.28

I think you are inviting confusion by naming your function inputs with the same names as columns that are in your example data.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.