See the FAQ: How to do a minimal reproducible example reprex
for beginners to attract answers that may conform more closely to your data.
suppressPackageStartupMessages({
library(dplyr)
library(lubridate)
})
dat <- data.frame(from = c("europe","africa","asia"),
birthdate = c("2010-01-11","2011-01-11","2011-01-12"),
happiness = c(1,2,3),
hue = c("mauve","fuscia","magenta"))
cats_clean <- function(x,y = "clear") {
if(is.na(y)) ret = x %>% group_by(hue) %>% summarize(happiness = sum(happiness, na.rm = TRUE))
if((y != "gray" & y != "orange")) ret = x %>% group_by(hue) %>% summarize(happiness = sum(happiness, na.rm = TRUE))
if(y == "gray") ret = x %>% dplyr::filter(from == "europe")
if(y == "orange") ret = x %>% filter(birthdate >= ymd("2011-01-11"))
return(ret)
}
cats_clean()
#> Error in group_by(., hue): argument "x" is missing, with no default
cats_clean(dat)
#> # A tibble: 3 × 2
#> hue happiness
#> <chr> <dbl>
#> 1 fuscia 2
#> 2 magenta 3
#> 3 mauve 1
cats_clean(dat,"red")
#> # A tibble: 3 × 2
#> hue happiness
#> <chr> <dbl>
#> 1 fuscia 2
#> 2 magenta 3
#> 3 mauve 1
cats_clean(dat,"gray")
#> from birthdate happiness hue
#> 1 europe 2010-01-11 1 mauve
cats_clean(dat,"orange")
#> from birthdate happiness hue
#> 1 africa 2011-01-11 2 fuscia
#> 2 asia 2011-01-12 3 magenta
Created on 2022-12-13 by the reprex package (v2.0.1)