按唯一ID为组填写缺失数据 [英] fill in missing data for group by unique ID

查看:48
本文介绍了按唯一ID为组填写缺失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的临床数据结构如下所示:

My clinical data structure looks like this:

patientid <- c(100,100,100,101,101,101,102,102,102,104,104,104)
group <- c(1,1,NA,2,NA,NA,1,1,1,2,2,NA)

Data<- data.frame(patientid=patientid,group=group)

如果缺少数据,则 NA 应与同一患者 ID 的其他组值相同.换句话说,患者总是在同一组中,需要填写缺失的数据以反映这一点.所以它应该是这样的:

If there is missing data then the NA should become the same value as the other group value for the same patient id. In other words a patient is always in the same group and the missing data needs to be filled in to reflect that. So it should look like this:

patientid <- c(100,100,100,101,101,101,102,102,102,104,104,104)
group <- c(1,1,1,2,2,2,1,1,1,2,2,2)

Data<- data.frame(patientid=patientid,group=group)

推荐答案

你可以写一个小辅助函数,如:

You can write a little helper function like:

fun <- function(x) replace(x, is.na(x), x[!is.na(x)][1])

然后,您可以在 base R 中的 transformwithin 中使用它:

Then, you can use it in transform or within in base R:

transform(Data, group = ave(group, patientid, FUN = fun))
#    patientid group
# 1        100     1
# 2        100     1
# 3        100     1
# 4        101     2
# 5        101     2
# 6        101     2
# 7        102     1
# 8        102     1
# 9        102     1
# 10       104     2
# 11       104     2
# 12       104     2

甚至与其他包:

library(data.table)
as.data.table(Data)[, group := fun(group), patientid][]

即使组值不是每个患者patientid"的第一个值,这也将起作用.试试,例如:

This will work even if the group value is not the first value for each patient "patientid". Try, for example:

# First row of "group" is `NA`
Data <- Data[c(3, 1, 2, 4:nrow(Data)), ]

这篇关于按唯一ID为组填写缺失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆