我在R中有一个以长格式表示的多变量重复测量数据集。时间变量包括基线,干预后和随访。我想基于基线值计算分位数,以查看这些分位数如何随时间发展。
I am using a quartile function I found on StackOverflow, which is working well.
getQuantileGroupNum <- function(vec, group_num, decreasing=FALSE) {
if(decreasing) {
abs(cut(vec, quantile(vec, probs=seq(0, 1, 1 / group_num), type=8, na.rm=TRUE), labels=FALSE, include.lowest=T) - group_num - 1)
} else {
cut(vec, quantile(vec, probs=seq(0, 1, 1 / group_num), type=8, na.rm=TRUE), labels=1:4, include.lowest=T)
}
}
我想计算基线分位数,并将其纳入我的长格式数据框中。因此,我过滤了基线值,并将四分位数函数应用于所有以_log结尾的变量。
df_q <- df %>%
filter(., time=="Baseline") %>%
dplyr::select(ends_with("_log")) %>%
cbind(., setNames(lapply(., getQuantileGroupNum, 4),
paste0(names(.), "_q")))
df_q现在是一个数据文件,仅包含多个对数转换变量的基线值和分位数。
问题: 1.如何将基线分位数变量合并到长格式数据文件df中?我可以使用rbind(df_q,df_q,df_q)和cbind将df_q添加到df中,但是有没有更清洁的方法? 2.我编写的脚本删除了该函数未应用到的所有变量(即时间,id)。如何不计算分位数就将这些变量保留在df_q中?选择将删除所有不以日志结尾的变量。
I've found a similar question elsewhere, but still couldn't figure it out.
感谢您的帮助。