0

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

Community
  • 1
  • 1
Pete855217
  • 1,570
  • 5
  • 23
  • 35
  • 1
    how about `?mapply`: "mapply is a multivariate version of sapply" – shadow Aug 01 '13 at 10:54
  • `sapply` or `mapply` is not the right approach here. R uses *pass-by-value*, not *pass-by-reference*. That means `zeroTime` does not modify its `x` and `y` arguments outside the scope of the function. @Karl's answer is the right approach. – flodel Aug 01 '13 at 11:12
  • Thanks flodel, that makes sense, if the functions just pass by value and have to return the updated variable. Thomas thanks for that too - I hadn't seen that question in my search. – Pete855217 Aug 01 '13 at 11:49

1 Answers1

4

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
Karl Forner
  • 4,175
  • 25
  • 32