0

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!

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
Hannah
  • 11
  • 2
  • When I do run it by replacing titanic with "." I just get this error: Error in nrow(x) : object 'x' not found – Hannah Aug 20 '21 at 21:20
  • titanic %>% filter(Age >= 30) %>% riskratio(table(.$Sex, .$Survived)) is specifically what I ran – Hannah Aug 20 '21 at 21:28
  • 1
    Since my "." idea was a flop, a different idea, maybe something like riskratio(table(titanic %>% filter(age >= 20) %>%select(Sex, Survived))) – Silentdevildoll Aug 20 '21 at 21:31
  • 1
    Shouldn't just piping it together like `titanic %>% filter(age >= 20) %>% select(Sex, Survived) %>% table() %>% riskratio()` work? – Martin Gal Aug 20 '21 at 21:56

0 Answers0