Change from baseline for repeated ids with missing baseline points

霸气de小男生 提交于 2019-12-12 18:29:45

问题


Change from baseline for repeated ids with missing baseline points

A similar question has been asked and answered below:

Change from baseline for repeated ids

My question differs from the original question in that I have missing baseline values. I am including a small reproducible example below:

df1 <- data.frame( probeID = c( rep("A", 19), rep("B",19), rep("C",19)),
                   Subject_ID = c( rep( c( rep(1,5), rep(2,4), rep(3,5), rep(4,5)),3)),
                   time = c(rep( c( c(1:5), c(2:5), rep( 1:5,2)),3)))
df1$measure <- df1$Subject_ID*c( 1:nrow(df1))

df2 <- subset( df1, Subject_ID != 2)

df2 %>%
  group_by(probeID, Subject_ID) %>%
  mutate(change = measure - measure[time==1])

However, when I replace df2 with df1 in the pipe above, it fails because data is missing for the time = 1 data point for Subject_ID=2. My desired output in the df1 case should be be identical to the output from df2. I would appreciate any help.

Thanks

JJ


回答1:


Was having some trouble trying to figure out what your question was asking for, does this work?

df1 %>%
  group_by(probeID, Subject_ID) %>%
  mutate(change = measure - first(measure))


来源:https://stackoverflow.com/questions/41604408/change-from-baseline-for-repeated-ids-with-missing-baseline-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!