I have a question regarding the following code:
Subjects <- unique(Dataset$ID)
for (i in Subjects){
startdate <- head(Dataset$DATE[i])
enddate <- tail(Dataset$DATE[i])
seq_date <- seq(as.Date(startdate), as.Date(enddate), "days")
}
With this code I want to obtain a startdate, enddate and seq_date for each unique individual. However, I only get one startdate (from all the first individual) and one enddate (from the last individual). Next to the above code I have tried the following code too:
Subjects <- unique(Dataset$ID)
for (i in Subjects){
startdate[i] <- head(Dataset$DATE[i])
enddate[i] <- tail(Dataset$DATE[i])
seq_date[i] <- seq(as.Date(startdate[i]), as.Date(enddate[i]), "days")
}
But this results in the error: Error in seq.int(0, to0 - from, by) : 'to' must be a finite number
How can I make this for loop work so that I get a startdate, enddate and seq_date for each individual?