gecko
gecko

Reputation: 153

Subtract V1 and V0 from other visit values in long format using grouping

I have a longitudinal dataset in long format and I am want to create two variables, one where visit=1 value for a given ID is subtracted from all other values and one where visit=0 value for a given ID is subtracted from all other values. Sample dataset:

df<-data.frame(value=rnorm(30), 
               visit=rep(seq(0:9), 3), i
               d=c(rep("A", 10), rep("B", 10), rep("C", 10)))

I know one method is to use the following code:

df %>% group_by(id) %>% 
       arrange(visit, .by_group=TRUE) %>%
       mutate(value_chg_v1=value-nth(value, n=2),
              value_chg_v0=value-nth(value, n=1))

but I want to construct the code so that if a entire row is missing for visit=0, value_chg_v0=NA. Is there a way to do this that does not involve adding in the rows where visit=0 is missing? Thanks in advance.

Upvotes: 1

Views: 308

Answers (1)

akrun
akrun

Reputation: 887391

We could use complete to create the missing combinations

library(dplyr)
library(tidyr)
df %>% 
   complete(id, visit = 0:9) %>% 
   group_by(id) %>%
   arrange(visit, .by_group=TRUE) %>%
   mutate(value_chg_v1=value-nth(value, n=2),
          value_chg_v0=value-nth(value, n=1)) %>%
   ungroup %>%
   drop_na(value)

Upvotes: 1

Related Questions