Hello,
In the first code chunk, I'm trying to use the dplyr ifelse() function to calculate a summary mean value based on a condition. The code runs, but I am getting incorrect values returned (the correct values are produced in the second chunk of code, by filtering the df first, then running the summary). The sum ifelse() works fine to get the count of rows passing the condition, and they match the values returned in the filtered code chunk.
I suspect I'm doing something wrong with the second argument of the ifelse() and have unsuccessfully tried several options, and would appreciate any help.
library(tidyverse)
library(nycflights13)
flt_late_b <- flights %>%
group_by(origin) %>%
summarise(
late_mean_delay = mean( ifelse( dep_delay >= 3.00, dep_delay, 0), na.rm = TRUE),
count_late = sum( ifelse( dep_delay >= 3.00, 1, 0), na.rm = TRUE)
) %>%
print('flt_late_b')
flt_late <- flights %>%
filter(dep_delay >= 3.00) %>%
group_by(origin) %>%
summarise(
late_ave_delay = mean( dep_delay, na.rm = TRUE),
count_late = n()
) %>%
print( 'flt_late')