1

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:

data view

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.

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
  • Please don't post pictures of your data. Include a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and expected output. – markus Aug 04 '18 at 10:45

1 Answers1

0

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
lokxs
  • 171
  • 10