Tibble titanic dataset

Hey I would like to do the following activities with the dataset titanic in tibble form:

Using the %>% operator and functions from dplyr:

  • Select the adult partial dataset,

  • group this subset by class and gender,

  • determine the relative survival frequency in each group and the number of observations per group.

I have already implemented the first two points as followed. However, I am not getting anywhere with the third point. Could someone help me? Thank you!

titanic_tibble <- as_tibble(datasets::Titanic)
titanic_tibble %>%
  filter(Age == "Adult") %>%
  group_by(Class,Sex) %>%
  summarise (n = n())

Hi there, how does this work for you? Converting Survived to 1/0 makes this easier:

titanic_tibble %>%
    mutate(n_surv = n * ifelse(Survived == "Yes",1,0)) %>%
    filter(Age == "Adult") %>%
    group_by(Class,Sex) %>%
    summarise (n = sum(n, na.rm = TRUE),
               n_surv = sum(n_surv, na.rm = TRUE),
               rate_surv = n_surv/n)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.