Consider the following dataset. It concerns the final date of the unemployment spell, but for now it can simply be seen as changing an enddate when certain characteristics are met.
df<-data.frame( c("R007", "R008", "R009", "R010"),
c(20130101, 20130201 ,20130301, 20130610),
c(20141231, 20141031, 20141231,20141231),
c(NA, NA, 20151231, 20151232),
c(NA, 20161001, 20160601, 20161010),
c(NA, 20170101, 2171201, 20171101))
colnames(df)<-c("id", "beginui", "endui.year1", "endui.year2", "endui.year3", "endui.year4")
df$beginui<-as.character(df$beginui)
df$beginui<-as.Date(df$beginui, "%Y%m%d")
df$endui.year1<-as.character(df$endui.year1)
df$endui.year1<-as.Date(df$endui.year1, "%Y%m%d")
df$endui.year2<-as.character(df$endui.year2)
df$endui.year2<-as.Date(df$endui.year2, "%Y%m%d")
df$endui.year3<-as.character(df$endui.year3)
df$endui.year3<-as.Date(df$endui.year3, "%Y%m%d")
df$endui.year4<-as.character(df$endui.year4)
df$endui.year4<-as.Date(df$endui.year4, "%Y%m%d")
#create an enddate variable
df$enddate<-df$endui.year1
#replace the enddate when certain conditions are met
#this one runs
#purpose of this line: change the enddate of the date in year 1 equals 2014/12/31 by the date in #year 2.
df$enddate[df$endui.year1==as.Date("2014-12-31") & !is.na(df$endui.year2)]<-df$endui.year2[!is.na(df$endui.year2)]
#this one does not run.
#Error in NextMethod(.Generic): NAs are not allowed in subscripted assignments
#purpose if this line: change the enddate to the variable in year 3 if the final data equals 2015-12-31
df$enddate[df$endui.year2==as.Date("2015-12-31") & !is.na(df$endui.year3)]<-df$endui.year3[!is.na(df$endui.year3)]
As already is inidicated in the line of code, the second condition does not run. And I do not know why. I checked here (NAs are not allowed in subscripted assignments) on this website to make sure I did not select NAs. However, appearently I miss something in the second line and I do not see what.