I want to work out whether a value in a grouping is different enough from other values in a grouping. Specifically I want to work out whether an end time of a matches with the start time of another lesson on the same day for the same student. Using diamonds, this is the equivalent code:
library(ggplot2)
diamonds %>% group_by(color, cut) %>%
mutate(clash = sum(
lapply(
diamonds %>%
filter(color == color, cut == cut, carat != carat) %$% carat,
function(x) ifelse(x < carat - 0.01 && x > carat + 0.01, 1, 0)))) %>%
arrange(color, cut, clash)
The plan is if clash is over 1, then I know that another diamond is very close in carat size to the diamond in that grouping. This gives me the following error:
Error in sum(sapply(diamonds %>% filter(color == color, cut == cut, carat != :
invalid 'type' (list) of argument
This makes the second call to diamond look dodgy