Reputation: 564
I have a simple data structure with id and time-series indicator (prd). I would like to create a dummy variable for followup visits "fup", which is equal to 0 if a patient has no more visits and 1 if a patient has more visits in the future.
How would I go about doing this?
id<- c(1, 1, 1, 2, 3, 3)
prd <- c(1, 2, 3, 1, 1, 2)
df <- data.frame(id=id, prd=prd)
Desired output:
id prd fup
1 1 1 1
2 1 2 1
3 1 3 0
4 2 1 0
5 3 1 1
6 3 2 0
Upvotes: 0
Views: 44
Reputation: 389155
We can check if the current row is the last row in each group. In base R,
df$fup <- with(df, ave(prd, id, FUN = function(x) seq_along(x) != length(x)))
df
# id prd fup
#1 1 1 1
#2 1 2 1
#3 1 3 0
#4 2 1 0
#5 3 1 1
#6 3 2 0
Similarly in dplyr
,
library(dplyr)
df %>% group_by(id) %>% mutate(fup = +(row_number() != n()))
and data.table
library(data.table)
setDT(df)[, fup := +(seq_along(prd) != .N), by = id]
Upvotes: 3