[英]How to return the vaules of several variables based on the minimum value in a specific group
I'm working with the database in the titanic library.我正在使用泰坦尼克号图书馆中的数据库。
The columns names (variables) are as follows: Age Name (string) String (integer 0/1)列名(变量)如下: Age Name (string) String (integer 0/1)
I would like to find the youngest and the oldest passenger who survived and to print their Name with their age.我想找到幸存的最年轻和最年长的乘客,并用他们的年龄打印他们的名字。 The bold one it the part I can't to get around.大胆的是我无法绕过的部分。
This is the code I came up with to find the min/max Age of survivors df = passengers2这是我想出的代码,用于查找幸存者的最小/最大年龄 df = passengers2
min(passengers2[passengers2$Survived==1,]$Age, na.rm=TRUE)
max(passengers2[passengers2$Survived==1,]$Age, na.rm=TRUE)
one way, using {dplyr} and the pipe:一种方法,使用 {dplyr} 和 pipe:
library(titanic)
library(dplyr)
titanic_train |>
filter(Survived == 1) |>
slice(which.min(Age), which.max(Age)) |>
select(Name, Age)
Name Age
1 Thomas, Master. Assad Alexander 0.42
2 Barkworth, Mr. Algernon Henry Wilson 80.00
evaluate per sex:按性别评估:
titanic_train |>
filter(Survived == 1) |>
group_by(Sex) |>
slice(which.min(Age), which.max(Age)) |>
select(Name, Age)
# A tibble: 4 x 3
# Groups: Sex [2]
Sex Name Age
<chr> <chr> <dbl>
1 female Baclini, Miss. Helene Barbara 0.75
2 female Andrews, Miss. Kornelia Theodosia 63
3 male Thomas, Master. Assad Alexander 0.42
4 male Barkworth, Mr. Algernon Henry Wilson 80
etc.等等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.