Вопрос

I have a similar, but not quite the same question to R apply function with multiple parameters

I have a dataset with two variables RSHIFTSTART and RSHIFTEND (amongst other variables). These variables contain times eg. 23:30:00 or 00:00:00. I'd like to replace RSHIFTSTART and RSHIFTEND with NA wherever BOTH variables are zero ie. '00:00:00'.

I've written a function which I think may do the job:

# Change '00:00:00' to NA where both variables are '00:00:00'
zeroTime=function(x,y) {
    if (x=='00:00:00' & y=='00:00:00') {
        replace(x,x=='00:00:00',NA)
        replace(y,x=='00:00:00',NA)
    }
}

My question is how to apply this function to update the dataset's variable ie. supplying two arguments to it. I tried:

sapply(rosterSample$RSHIFTSTART,rosterSample$RSHIFTEND,zeroTime)

but this syntax is incorrect. Perhaps I'll be restricted to changing just one variable per call ie. RSHIFTSTART or RSHIFTEND, that's OK.

Any ideas? Thanks Pete

Это было полезно?

Решение

Something like this ?

rosterSample <- data.frame(RSHIFTSTART=c('00:00:00', '01:00:00', '00:00:00'), RSHIFTEND=c('10:00:00', '00:00:00', '00:00:00'), stringsAsFactors=FALSE)
ind <- with(rosterSample, RSHIFTSTART=='00:00:00' & RSHIFTEND=='00:00:00')
rosterSample$RSHIFTSTART[ind] <- NA
rosterSample$RSHIFTEND[ind] <- NA
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top