I have the following data frame:
df = structure(list(age = c("F", "F", "M", "M", "M", "F", "M", "M",
"F", "F", "M", "M", "F", "F", "F", "F", "F", "M", "M", "F", "F"
), gender = c(52.8547945205479, 70.617475870193, 47.6986301369863,
85.4876712328767, 56.0288204261033, 27.0219178082192, 40.8583963494959,
24.6553462722298, 80.4027397260274, 55.6684931506849, 70.6904109589041,
64.5095890410959, 45.5397260273973, 78.5909038861022, 42.4219178082192,
44.0712328767123, 77.7068493150685, 70.5199279905761, 43.7178082191781,
77.7205479452055, 74.972602739726)), row.names = c(NA, -21L), class = c("tbl_df",
"tbl", "data.frame"))
I would like to filter, by gender, the ages which are greater than the mean age for that gender. But I would like to do this by column number rather than name.
So I tried:
df %>% group_by_at(1) %>% filter_at(vars(2) > mean(vars(2))
But this didn't work.
Any suggestions?