Change from baseline for repeated ids with missing baseline points
Asked Answered
J

1

2

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

Jenette answered 12/1, 2017 at 3:0 Comment(0)
C
0

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))
Centeno answered 12/1, 2017 at 5:43 Comment(1)
This requires the columns to be ordered, so you may need arrange(probeID, Subject_ID) beforehandCenteno

© 2022 - 2024 — McMap. All rights reserved.