How do I filter data based on some condition, and then run a function on the filtered dataset.
For example: Say I’m looking at the titanic dataset
library(titanic)
library(epitools)
and I want to look at risk ratios of males compared to females of surviving. But I only want to look at males and females over the age of 20.
I know I can do it like this
titanic_filtered <- titanic %>% filter(age >= 20)
riskratio(table(titanic_filtered$Sex, titanic_filtered$Survived))
However I don’t want to have create a filtered dataset every time. Is there a way to do this in one line?
So ideally it would be something like
titanic %>% filter(age >= 20) %>% riskratio(table(titanic$Sex, titanic$Survived))
However this wouldn’t work as it wouldn’t use the filtered dataset. Is there a way to do this in one line without saving the filtered dataset to a new name and using that?
Any help would be appreciated! Thanks!