在我的一项分析中,我想评估不同受试者(id)在一定时间内的血红蛋白水平(art_hb)过程(nmp_time)。我们想要把id在不同类别的血红蛋白水平(0-3;3-6;6-9和>9),根据其第一次测量。因此,如果血红蛋白水平随着时间的推移而变化,我们不希望它改变类别。
我的示例数据:df <- data.frame(id=factor(c(1,1,1,2,2,2)), time=c(0,30,60,0,30,60), art_hb=c(5.8,6.1,5.9,6.7,6.9,NA))
到目前为止,我已经在time==0 df$art_hb_cat <- ifelse(df$art_hb < 3 & df$time == 0, "0-3", ifelse(df$art_hb >= 3 & df$art_hb < 6 & df$time == 0, "3-6", ifelse(df$art_hb >= 6 & df$art_hb < 9 & df$time == 0, "6-9", ifelse(df$art_hb > 9 & df$time == 0, ">9", ""))))
的血红蛋白测量的基础上创建了分类
这导致:df <- data.frame(id=factor(c(1,1,1,2,2,2)), time=c(0,30,60,0,30,60), art_hb=c(5.8,6.1,5.9,6.7,6.9,NA)), art_hb_cat=c("3-6","","","6-9","","")
现在,我想为id (-> group_by(id))复制这些类别,最后得到一个df,比如:df <- data.frame(id=factor(c(1,1,1,2,2,2)), time=c(0,30,60,0,30,60), art_hb=c(5.8,6.1,5.9,6.7,6.9,NA)), art_hb_cat=c("3-6","3-6","3-6","6-9","6-9","6-9")
但在尝试了几天之后,我还是没能做到。有人能帮我吗?非常,非常感谢。
这是我的第一篇帖子,所以我希望这足够清楚。抱歉的。
发布于 2022-08-29 15:38:10
我们可以在按'id‘分组后使用case_when
library(dplyr)
df %>%
group_by(id) %>%
mutate(art_hb_cat = case_when(art_hb < 3 & time == 0 ~ "0-3", art_hb >=3 & art_hb <6 & time == 0 ~ "3-6", art_hb>=6 & art_hb < 9 & time == 0 ~ "6-9", art_hb > 9 & time == 0 ~ ">9")[1]) %>%
ungroup
-output
# A tibble: 6 × 4
id time art_hb art_hb_cat
<fct> <dbl> <dbl> <chr>
1 1 0 5.8 3-6
2 1 30 6.1 3-6
3 1 60 5.9 3-6
4 2 0 6.7 6-9
5 2 30 6.9 6-9
6 2 60 NA 6-9
或使用data.table
library(data.table)
setDT(df)[df[time == 0, .(id, art_hb_cat = fcase(between(art_hb, 0,
3), "0-3", between(art_hb, 3, 6), "3-6", between(art_hb, 6, 9),
"6-9", default = ">9"))], on = .(id)]
id time art_hb art_hb_cat
<fctr> <num> <num> <char>
1: 1 0 5.8 3-6
2: 1 30 6.1 3-6
3: 1 60 5.9 3-6
4: 2 0 6.7 6-9
5: 2 30 6.9 6-9
6: 2 60 NA 6-9
发布于 2022-08-29 15:37:33
您可以使用cut
而不是ifelse
,并将其应用于art_hb
when time == 0
for group
of id
library(dplyr)
df %>%
group_by(id) %>%
mutate(art_hb_cat = cut(art_hb[time == 0],
breaks = c(0, 3, 6, 9, Inf),
labels = c("0-3", "3-6", "6-9", ">9")))
id time art_hb art_hb_cat
<fct> <dbl> <dbl> <fct>
1 1 0 5.8 3-6
2 1 30 6.1 3-6
3 1 60 5.9 3-6
4 2 0 6.7 6-9
5 2 30 6.9 6-9
6 2 60 NA 6-9
https://stackoverflow.com/questions/73531157
复制相似问题