samedi 23 septembre 2017

R if else when trying to populate cell

fdI'm working with R and this is my dilemma.

The gist of the story is I"m taking two different times, splitting the times in the middle and would like to create a separate column with the split times. I have that worked out with this formula:

 burglary$midtime <- as.POSIXct((as.numeric(burglary$BegDate) + as.numeric(burglary$EndDate)) / 2, origin = '1970-01-01')

The hiccup is not all the times have and end time. So if it doesn't have and end time I want to populate it with just the beginning time. I came up with teh below if / else statement but it just populates the column with the split times and leaves the others blank.

if(!is.null(burglary$EndDate)) { 
    burglary$midtime <- as.POSIXct(burglary$BegDate)
} else {
    burglary$midtime <- as.POSIXct((as.numeric(burglary$BegDate) +    
        as.numeric(burglary$EndDate)) / 2, origin = '1970-01-01')
}

Any idea what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire