I have function that takes a dataframe and two other variables (horse and race_date) as inputs. The horse and race_date are utilized to filter the dataframe passed the function and then a summarise function is applied to calculate the desired output. When I test the function stand-alone and outside of the pipes everything works fine but when I try to run the function from within a mutate function and the pipes I get the following error message:
Error: Problem with `mutate()` input `split_Lt`.
x Problem with `filter()` input `..1`.
x Input `..1` must be of size 1, not size 18.
i Input `..1` is `Horse == horse & NewSplit == "LT Races" & race_date < date`.
i The error occurred in group 2: split = "A BIT OF BOTH_var106_Track: CD".
i Input `split_Lt` is `getsplit_LT(splits, horse, race_date)`.
i The error occurred in group 2: split = "A BIT OF BOTH_var106_Track: CD".
Here is the function:
getsplit_LT <- function(df, horse, date){
kpi <- df %>%
filter(Horse == horse & NewSplit == "LT Races" & race_date < date) %>%
group_by(split) %>%
summarise_if(is.numeric, sum) %>%
mutate(TopAvgB = ((E + 3.439) /(R+3.439 + 25.69))) %>%
select(TopAvgB)
x = if(is.data.frame(kpi) && nrow(kpi)==0){0}else{kpi[[1]]}
return(x)
}
Here's the code I tried to run:
df <- df %>%
mutate(split_Lt = getsplit_LT(splits, horse, race_date))
Here is the dput data:
structure(list(horse = c("A BIT OF BOTH", "A BIT OF BOTH", "A BIT OF BOTH",
"A BIT OF BOTH", "A BIT OF BOTH", "A BIT OF BOTH", "A BIT OF BOTH",
"A BIT OF BOTH", "A BIT OF BOTH", "A BIT OF BOTH", "A BIT OF BOTH",
"A BIT OF BOTH", "A BIT OF BOTH", "A BIT OF BOTH", "A BIT OF BOTH",
"A BIT OF BOTH", "A BIT OF BOTH", "A BIT OF BOTH"), race_date = structure(c(17802,
17906, 17941, 17969, 18006, 18062, 18091, 18183, 18183, 18226,
18244, 18286, 18454, 18502, 18546, 18581, 18601, 18664), class = "Date")), row.names = c(NA,
-18L), groups = structure(list(horse = "A BIT OF BOTH", .rows = structure(list(
1:18), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr",
"list"))), row.names = 1L, class = c("tbl_df", "tbl", "data.frame"
), .drop = TRUE), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
))
structure(list(split = c("A BIT OF BOTH_var102B_LifeTime: Life",
"A BIT OF BOTH_var102B_LifeTime: Life", "A BIT OF BOTH_var102B_LifeTime: Life",
"A BIT OF BOTH_var102B_LifeTime: Life", "A BIT OF BOTH_var102B_LifeTime: Life",
"A BIT OF BOTH_var102B_LifeTime: Life", "A BIT OF BOTH_var102B_LifeTime: Life",
"A BIT OF BOTH_var102B_LifeTime: Life", "A BIT OF BOTH_var102B_LifeTime: Life",
"A BIT OF BOTH_var102B_LifeTime: Life", "A BIT OF BOTH_var102B_LifeTime: Life",
"A BIT OF BOTH_var102B_LifeTime: Life", "A BIT OF BOTH_var102B_LifeTime: Life",
"A BIT OF BOTH_var102B_LifeTime: Life", "A BIT OF BOTH_var102B_LifeTime: Life",
"A BIT OF BOTH_var102B_LifeTime: Life", "A BIT OF BOTH_var102B_LifeTime: Life",
"A BIT OF BOTH_var102B_LifeTime: Life", "A BIT OF BOTH_var106_Track: CD",
"A BIT OF BOTH_var106_Track: CT", "A BIT OF BOTH_var106_Track: DE",
"A BIT OF BOTH_var106_Track: FG", "A BIT OF BOTH_var106_Track: GP",
"A BIT OF BOTH_var106_Track: GP", "A BIT OF BOTH_var106_Track: GP",
"A BIT OF BOTH_var106_Track: GP", "A BIT OF BOTH_var106_Track: GP",
"A BIT OF BOTH_var106_Track: GP", "A BIT OF BOTH_var106_Track: GP",
"A BIT OF BOTH_var106_Track: GP", "A BIT OF BOTH_var106_Track: KE",
"A BIT OF BOTH_var106_Track: MT", "A BIT OF BOTH_var106_Track: MT",
"A BIT OF BOTH_var106_Track: OT", "A BIT OF BOTH_var106_Track: PX",
"A BIT OF BOTH_var106_Track: PX", "A BIT OF BOTH_var107_Surface: Dirt",
"A BIT OF BOTH_var107_Surface: Dirt", "A BIT OF BOTH_var107_Surface: Dirt",
"A BIT OF BOTH_var107_Surface: Dirt", "A BIT OF BOTH_var107_Surface: Dirt",
"A BIT OF BOTH_var107_Surface: Dirt", "A BIT OF BOTH_var107_Surface: Dirt",
"A BIT OF BOTH_var107_Surface: Dirt", "A BIT OF BOTH_var107_Surface: Dirt",
"A BIT OF BOTH_var107_Surface: Dirt", "A BIT OF BOTH_var107_Surface: Dirt",
"A BIT OF BOTH_var107_Surface: Dirt", "A BIT OF BOTH_var107_Surface: Dirt",
"A BIT OF BOTH_var107_Surface: Synth", "A BIT OF BOTH_var107_Surface: Turf",
"A BIT OF BOTH_var107_Surface: Turf", "A BIT OF BOTH_var107_Surface: Turf"
One approach is with the purrr::pmap
function which applies a function on a data.frame rowwise.
library(tidyverse)
pmap(df, ~ getsplit_LT(splits, horse = .x, date = .y))
[[1]]
[1] 0.2156712
[[2]]
[1] 0
[[3]]
[1] 0.1070373
[[4]]
[1] 0.1339914
[[5]]
[1] 0.1593659
...
Or back with the original data.frame:
bind_cols(df,kpi = pmap_dbl(df, ~ getsplit_LT(splits, horse = .x, date = .y)))
# A tibble: 18 x 3
horse race_date kpi
<chr> <date> <dbl>
1 A BIT OF BOTH 2020-09-28 0.216
2 A BIT OF BOTH 2020-01-10 0
3 A BIT OF BOTH 2020-02-14 0.107
4 A BIT OF BOTH 2020-03-14 0.134
5 A BIT OF BOTH 2020-04-20 0.159
6 A BIT OF BOTH 2020-06-15 0.183
7 A BIT OF BOTH 2020-07-14 0.227
...
Data:
splits <- read_csv("https://raw.githubusercontent.com/Handicappr/Rstudio_test_project/main/splits.csv")
df <- read_csv("https://raw.githubusercontent.com/Handicappr/Rstudio_test_project/main/df.csv")
splits %>% mutate(race_date = as.Date(race_date,"%m/%d/%y")) -> splits
df %>% mutate(race_date = as.Date(race_date,"%m/%d/%y")) -> df