简体   繁体   中英

Creating dummy variable in R for panel data which is sensitive to past years

So essentially I was looking to create a variable in R (lets call it historyD) which == 1 if another dummy (let's call it currentD) in the panel data == 1 for the current year or == 1 if currentD == 1 for any of the past years specific to each participant. Tricky part is my data is sorted into participants and descending years, so I wanted it to look like this:

资料检视

Finding it a hard time to match the code to each participant, so that the historyD relies on current and past year currentD specific for each participant. Have made very little progress so far.

Any help would be greatly appreciated.

Thanks.

If indeed your data is in ascending order of Participant and then in DESCENDING order of Year , first you can sort it by ascending order of Participant and then in ASCENDING order of Year

# creation of dataframe
Participant <- factor(rep(c(1,2,3), each = 4))
Year <- rep(c(4,3,2,1), 3) #descending order of the years
currentD <- c(0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1) #data rearranged to fit your data
DF <- data.frame(Participant, Year, currentD)

# reorder the DF with participant and year in ascending order
DF <- DF[  with(DF, order(Participant, Year)),   ]

# cumulative sum by group of *Participant*
DF$historyD <- unlist(by(DF$currentD, DF$Participant, cumsum))

# All numbers higher than 1 are reset to 1
DF[DF$historyD >1, "historyD"] <- 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM