Search code examples
rnaas.date

Dates error NAs are not allowed in subscripted assignments


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.


Solution

  • The expression:

    df$enddate[df$endui.year2 == as.Date("2015-12-31") & !is.na(df$endui.year3)]
    

    Produces NA values:

    #> [1] NA           "2015-12-31" NA  
    

    These go away if you wrap the logical condition in which

    df$enddate[which(df$endui.year2 == as.Date("2015-12-31") & !is.na(df$endui.year3))]
    #> [1] "2014-12-31"
    

    But you also need to have the same condition for the dates that you wish to move too (you need to select the endui.year2 value in the row where df$endui.year2 == as.Date("2015-12-31")) to move it to the correct place. Perhaps the easiest way to do this is to set the conditions first

    condition1 <- which(df$endui.year1 == as.Date("2014-12-31") & !is.na(df$endui.year2))
    df$enddate[condition1] <- df$endui.year2[condition1]
    
    condition2 <- which(df$endui.year2 ==a s.Date("2015-12-31") & !is.na(df$endui.year3))
    df$enddate[condition2] <- df$endui.year3[condition2]
    

    Which results in:

    df
    #>     id    beginui endui.year1 endui.year2 endui.year3 endui.year4    enddate
    #> 1 R007 2013-01-01  2014-12-31        <NA>        <NA>        <NA> 2014-12-31
    #> 2 R008 2013-02-01  2014-10-31        <NA>  2016-10-01  2017-01-01 2014-10-31
    #> 3 R009 2013-03-01  2014-12-31  2015-12-31  2016-06-01        <NA> 2016-06-01
    #> 4 R010 2013-06-10  2014-12-31        <NA>  2016-10-10  2017-11-01 2014-12-31