R:如何用不同的表达式总结几个变量,然后用一个表达式总结其余的
R: how to summarise several variables with different expressions and then one expression for the rest
假设我有以下数据集:
Lines <- "id time sex Age A B C
1 1 male 90 0 0 0
1 2 male 91 0 0 0
1 3 male 92 1 1 0
2 1 female 87 0 1 1
2 2 female 88 0 1 0
2 3 female 89 0 0 1
3 1 male 50 0 1 0
3 2 male 51 1 0 0
3 3 male 52 0 0 0
4 1 female 54 0 1 0
4 2 female 55 0 1 0
4 3 female 56 0 1 0"
我想对数据框进行分组,对于 id、time、sex 和 Age,我得到第一个值,而对于其余变量 A B C,我得到最大值。
Lines <- "id time sex Age A B C
1 1 male 90 1 1 0
2 1 female 87 0 1 1
3 1 male 50 1 1 0
4 1 female 54 0 1 0"
到目前为止我已经尝试过:
Lines %>% Lines
summarise(id = first(patient_id), time = first(time), sex = first(sex),
Age = first(Age), vars = max(vars))
我正在努力使用表达式来表征其余变量,例如 vars
。
你可以
library(dplyr)
Lines %>%
read.table(text = ., header = T) %>%
group_by(id) %>%
summarize(across(c(time, sex, Age), first),
across(-c(time, sex, Age), max))
回归
# A tibble: 4 x 7
id time sex Age A B C
<int> <int> <chr> <int> <int> <int> <int>
1 1 1 male 90 1 1 0
2 2 1 female 87 0 1 1
3 3 1 male 50 1 1 0
4 4 1 female 54 0 1 0
假设我有以下数据集:
Lines <- "id time sex Age A B C
1 1 male 90 0 0 0
1 2 male 91 0 0 0
1 3 male 92 1 1 0
2 1 female 87 0 1 1
2 2 female 88 0 1 0
2 3 female 89 0 0 1
3 1 male 50 0 1 0
3 2 male 51 1 0 0
3 3 male 52 0 0 0
4 1 female 54 0 1 0
4 2 female 55 0 1 0
4 3 female 56 0 1 0"
我想对数据框进行分组,对于 id、time、sex 和 Age,我得到第一个值,而对于其余变量 A B C,我得到最大值。
Lines <- "id time sex Age A B C
1 1 male 90 1 1 0
2 1 female 87 0 1 1
3 1 male 50 1 1 0
4 1 female 54 0 1 0"
到目前为止我已经尝试过:
Lines %>% Lines
summarise(id = first(patient_id), time = first(time), sex = first(sex),
Age = first(Age), vars = max(vars))
我正在努力使用表达式来表征其余变量,例如 vars
。
你可以
library(dplyr)
Lines %>%
read.table(text = ., header = T) %>%
group_by(id) %>%
summarize(across(c(time, sex, Age), first),
across(-c(time, sex, Age), max))
回归
# A tibble: 4 x 7
id time sex Age A B C
<int> <int> <chr> <int> <int> <int> <int>
1 1 1 male 90 1 1 0
2 2 1 female 87 0 1 1
3 3 1 male 50 1 1 0
4 4 1 female 54 0 1 0