Change from baseline for repeated ids with missing

2019-07-11 14:27发布

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条回答
SAY GOODBYE
2楼-- · 2019-07-11 14:29

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))
查看更多
登录 后发表回答