I would like to create a new variable that is labelled change_index. This variable is outcome1 at time 3 - outcome 1 at time 1 / outcome1 at time 1.
How do I go about doing this? I tried doing the following
outcome1t0 <- data %>%
filter(time == "1") %>%
select(outcome1)
outcome1t12 <- data %>%
filter(time == "3") %>%
select(outcome1)
data$newvariable <- (outcome1t0 - outcome1t12) / outcome1t0
but I get the following error
Error in `$<-.data.frame`(`*tmp*`, bicind, value = list(bicep = c(13.3591525423729, :
replacement has 20 rows, data has 60
I realize this happens because the new data frame is smaller since it contains less rows. Should I just create a new data frame with change index? How do I go about doing this?
I have to calculate this change index for many variables in columns (many outcomes). Is there a way to automate this process?
Thanks for reading.
subject treatment time outcome1 outcome2
1 1 a 1 80 15
2 1 a 2 75 14
3 1 a 3 74 12
4 2 b 1 90 16
5 2 b 2 81 15
6 2 b 3 76 15
EDIT 1
Tried doing the following as suggested below, I changed the names according to my data
ancestral1 %>%
group_by(subject) %>%
mutate(bicep0 = bicep[time == 0],
bicep12 = bicep[time == 12],
bicepind = (bicep12 - bicep0) / bicep12)
I get the following error
Error in mutate_impl(.data, dots) :
Column `bicep0` must be length 1 (the group size), not 0
EDIT 2
Tried the new suggestion, still the same error
ancestral1 %>%
group_by(subject) %>%
mutate(bicep0 = if(any(time == 5)) bicep[time == 5] else NA,
bicep12 = bicep[time == 3],
bicepind = (bicep0 - bicep12) / bicep0)
Error in mutate_impl(.data, dots) :
Column `bicep12` must be length 1 (the group size), not 0
filter, the number of rows differ for both objectsdata %>% group_by(subject) %>% mutate(outcome1t0 = if(any(time == 5)) outcome1[time == 5] else NA, outcome1t2 = outcome1[time == 3], newvariable = (outcome1t0 - outcome1t2) / outcome1t0)