【发布时间】:2020-10-19 00:11:53
【问题描述】:
我正在使用许多不同的 R 课程使用的“泰坦尼克号”包。不过,这对我来说非常困难,因为一个人是否幸存是由 0(没有幸存)或 1(幸存)决定的。我正在尝试创建一个基本的条形图,但似乎无法总结每个性别的 1 - 或任何其他变量。
library(tidyverse)
install.packages('titanic')
library(titanic)
只是一点点转变:
titanic <- titanic_train %>%
+ select(Survived, Pclass, Sex, Age, SibSp, Parch, Fare) %>%
+ mutate(Survived = factor(Survived),
+ Pclass = factor(Pclass),
+ Sex = factor(Sex))
我试图通过 summarise() 确定女性和男性幸存者的数量:
titanic %>% group_by(Sex) %>% summarise(., survived = count(Survived))
titanic %>% group_by(Sex) %>% summarise(., survived = sum(Survived))
我试图创建一个条形图:
ggplot(titanic, mapping = aes(Sex, Survived)) + geom_bar()
ggplot(titanic, mapping = aes(Sex, sum(Survived))) + geom_bar()
ggplot(titanic, mapping = aes(Sex, count(Survived))) + geom_bar()
有人能告诉我如何在泰坦尼克号数据集中使用像“Survived”这样的变量吗?
【问题讨论】: