Error: Problem with `mutate()` input `..1`

I don't know why, but not being able to generate the values ​​for one id/week/category/dt specifically, as you can see below, as well as not being able to generate for all data using my f2 function, it gives error in mutate.. What am I doing wrong?

Code executable

library(dplyr)

df1<-structure(list(Id = c(1, 1, 1, 1, 1, 1), date = structure(c(19090, 
19090, 19090, 19090, 19090, 19090), class = "Date"), date2 = structure(c(18962,18962, 18962, 18962, 18962, 18962), class = "Date"), 
Week = c("Wednesday","Wednesday", "Wednesday", "Wednesday", "Wednesday", 
"Wednesday"), DT = c(1, NA_character_, NA_character_,NA_character_, NA_character_, 1), Category = c("AB","CD", "EF", "GH", "IJ", "KL"), 
Time = c(1.86, 4, 2.89, 0, 1, 2.4)), row.names = c(NA, -6L), class = c("tbl_df","tbl", "data.frame"))

> df1
# A tibble: 6 x 7
     Id date       date2      Week      DT    Category  Time
  <dbl> <date>     <date>     <chr>     <chr> <chr>    <dbl>
1     1 2022-04-08 2021-12-01 Wednesday 1     AB        1.86
2     1 2022-04-08 2021-12-01 Wednesday NA    CD        4   
3     1 2022-04-08 2021-12-01 Wednesday NA    EF        2.89
4     1 2022-04-08 2021-12-01 Wednesday NA    GH        0   
5     1 2022-04-08 2021-12-01 Wednesday NA    IJ        1   
6     1 2022-04-08 2021-12-01 Wednesday 1     KL        2.4 
    

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

Generate to specific id/week/category/dt

f2(df1,"1","Wednesday","AB","1")
  Id      Week Category DT Time
  1  1 Wednesday       AB  1 1.86

f2(df1,"1","Wednesday","IJ",NA)
[1] Id       Week     Category DT       Time    
<0 linhas> (ou row.names de comprimento 0)

Generate to all

Result <- df1 %>%
rowwise()
mutate(f2(df1,Id,Week,Category, DT))%>%
data.frame()

Error: Problem with `filter()` input `..1`.
i Input `..1` is `Id == idd`.
x object 'Id' not found

Result correct output:

  Id      Week Category   DT Time
1  1 Wednesday       AB    1 1.86
2  1 Wednesday       CD <NA> 4.00
3  1 Wednesday       EF <NA> 2.89
4  1 Wednesday       GH <NA> 0.00
5  1 Wednesday       IJ <NA> 1.00
6  1 Wednesday       KL    1 2.40

The error is due to you didn't input any .data to mutate()
And I don't think there seems to be a need to use mutate to quote the functionf2 here. directly do:

df1 %>%
  group_by(Id,Week, Category,DT) %>% 
  summarise(across(matches(nms), mean, .names = 'Time',na.rm = TRUE), .groups = 'drop') %>% 
  mutate(Time = format(round(Time, digits = 2), nsmall = 2))

# A tibble: 6 x 5
     Id Week      Category DT    Time 
  <dbl> <chr>     <chr>    <chr> <chr>
1     1 Wednesday AB       1     1.86 
2     1 Wednesday CD       NA    4.00 
3     1 Wednesday EF       NA    2.89 
4     1 Wednesday GH       NA    0.00 
5     1 Wednesday IJ       NA    1.00 
6     1 Wednesday KL       1     2.40 

is feasible in my opinion.

Thanks for the answer, I thought the way you said it too, however, I need to somehow make it call the f2 function. Do you think it's possible?

it is possible, but requires some modifications.
Because na values are not able to do the logic operation therefore can't filter:

> NA==NA
[1] NA
> filter(df1,DT==NA)
# A tibble: 0 x 7
# ... with 7 variables: Id <dbl>, date <date>, date2 <date>, Week <chr>, DT <chr>, Category <chr>, Time <dbl>

So, I just replaced all NAs with character "NA".

f2 <- function(idd,ds,codagr,dt) {
  
  nms <- c('Time|time')
  
  mtime <- df1 %>% mutate(DT = replace_na(DT, "NA")) %>% 
    filter(Id==idd,Week == ds, Category == codagr,DT == dt) %>% 
    group_by(Id,Week,Category,DT) %>% 
    summarise(across(matches(nms), mean, .names = 'Time',na.rm = TRUE), .groups = 'keep') %>% 
    mutate(Time = format(round(Time, digits = 2), nsmall = 2))

  return(mtime)
}

df1 %>% mutate(DT = replace_na(DT, "NA")) %>% 
  rowwise %>% mutate(f2(idd=Id,ds=Week,codagr=Category, dt=DT),.keep = 'none')

# A tibble: 6 x 5
# Rowwise: 
     Id Week      DT    Category Time 
  <dbl> <chr>     <chr> <chr>    <chr>
1     1 Wednesday 1     AB       1.86 
2     1 Wednesday NA    CD       4.00 
3     1 Wednesday NA    EF       2.89 
4     1 Wednesday NA    GH       0.00 
5     1 Wednesday NA    IJ       1.00 
6     1 Wednesday 1     KL       2.40 

do filter in f2 before summarise may saves time.

1 Like

Thanks for reply @yifanliu

1 Like

This topic was automatically closed 7 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.