Data Table Solution To New Structured Variable
问题 data=data.frame("student"=c(1,1,1,1,2,2,2,2,3,3,4,4,4,4), "score"=c(1,2,1,1,2,3,2,NA,3,NA,1,3,2,1), "drop"=c(0,0,0,0,0,0,0,1,0,1,0,0,0,0), "WANT"=c(1,2,1,1,2,3,3,4,3,4,1,3,3,3)) I have dataframe 'data' sans 'WANT' which is what I hope to create using a data.table solution. The rules are: if score = 1, WANT = 1 if score = 2, WANT = 2 if score = 3, WANT = 3, if drop = 1, WANT=4 if score at t = 2 and score at t+1 = 1 that is ok but if score at t = 3 and score at any later scores are less than 3,