library(dslabs)
library(dplyr)
##
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readr)
library(titanic)
#datos
titanic <- titanic_train
##Sobrevivientes Titanic por clase
#hombres
titanic %>%
select(Sex, Survived,Age, Pclass) %>%
filter(Survived == 1, Sex=='male') %>%
count(Pclass)
## Pclass n
## 1 1 45
## 2 2 17
## 3 3 47
#Mujeres
titanic %>%
select(Sex, Survived,Age, Pclass) %>%
filter(Survived == 1, Sex=='female') %>%
count(Pclass)
## Pclass n
## 1 1 91
## 2 2 70
## 3 3 72
#Total
titanic %>%
select(Sex, Survived,Age, Pclass) %>%
filter(Survived == 1) %>%
count(Pclass)
## Pclass n
## 1 1 136
## 2 2 87
## 3 3 119
##Porcentaje de la posibilidad de sobrevivir segun el numero de familiares en el barco
titanic %>% mutate(FamilySize = (SibSp + Parch + 1)) %>% group_by(FamilySize) %>% summarise(survival_rate = round(mean(Survived)*100, digits = 2)) %>% arrange(desc(survival_rate))
## # A tibble: 9 × 2
## FamilySize survival_rate
## <dbl> <dbl>
## 1 4 72.4
## 2 3 57.8
## 3 2 55.3
## 4 7 33.3
## 5 1 30.4
## 6 5 20
## 7 6 13.6
## 8 8 0
## 9 11 0
##Cuantas personas con ‘John’ en su nombre sobrevivieron
titanic %>%
select(Name, Age, Survived, Sex) %>%
filter(Survived == 1 & grepl('John', Name, ignore.case = TRUE))
## Name Age Survived Sex
## 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) 38 1 female
## 2 Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) 27 1 female
## 3 Doling, Mrs. John T (Ada Julia Bone) 34 1 female
## 4 Goldsmith, Master. Frank John William "Frankie" 9 1 male
## 5 Johnson, Miss. Eleanor Ileen 1 1 female
## 6 Mellors, Mr. William John 19 1 male
## 7 Goldsmith, Mrs. Frank John (Emily Alice Brown) 31 1 female
## 8 Davies, Master. John Morgan Jr 8 1 male
## 9 Thayer, Mr. John Borland Jr 17 1 male
## 10 Flynn, Mr. John Irwin ("Irving") 36 1 male
## 11 Thayer, Mrs. John Borland (Marian Longstreth Morris) 39 1 female
## 12 Astor, Mrs. John Jacob (Madeleine Talmadge Force) 18 1 female
## 13 Hogeboom, Mrs. John C (Anna Andrews) 51 1 female
## 14 Johnson, Master. Harold Theodor 4 1 male