I am trying to create a new variable that is basically the starting value of another variable in my dataframe. Example data:
id <- rep(c(1, 2), each = 8)
outcome <- rep(1:5, length.out = 16)
time <- rep(c(0, 1, 3, 4),4)
Attitude <- rep(c('A1', 'A2', 'A1', 'A2'), each = 4)
df <- data.frame(id, Attitude, outcome, time)
What I'd like to get is a new column named new_var (or whatever) that is equal to the value of outcome
at time == 0
for id = id
and also depends on Attitude
. Thus what I'd like to extend the dataframe
to is:
df$new_var <- c(1,1,1,1,5,5,5,5,4,4,4,4,3,3,3,3)
Only then with some decent coding. In SAS I know I can do this with the lag
function. I would really appreciate a solution that isn't a 'work around' so it is like SAS, but rather the proper r solution. In the end I want to get stronger in r too.
Related: Retain and lag function in R as SAS However I prefer some solution that is based on indices or the 'usual' r way. And here it's also not dependent on other conditions.
So, important here is that the coding works for the different ids
, attitude
levels / variables (A1, A2, ...) and that the outcome value
at time == 0
is basically copied to new_var
.
I hope I am clear in conveying my message. If not I think the small piece of example code and how I'd like to extend it should be clear enough. Looking forward to suggestions.
EDIT Another example code for @jogo answer.
ID <- rep(1, 36)
Attitude <- rep(c('A1', 'A2','A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9'),
length.out =36)
Answer_a <- rep(1:5, length.out = 36)
time <- as.character(rep(c(0, 1, 3, 4), each = 9))
df <- data.frame(ID, Attitude, Answer_a, time)
df$time <- as.character(df$time)