This is weird: R's ifelse()
seems to do some (unwanted) casting: Lets say I have a vector of timestamps (possibly NA) and NA values should be treated differently than existing dates, for example, just ignored:
formatString = "%Y-%m-%d %H:%M:%OS"
timestamp = c(as.POSIXct(strptime("2000-01-01 12:00:00.000000", formatString)) + (1:3)*30, NA)
Now
timestamp
#[1] "2000-01-01 12:00:30 CET" "2000-01-01 12:01:00 CET" "2000-01-01 12:01:30 CET"
#[6] NA
as desired but translation by 30 seconds results in
ifelse(is.na(timestamp), NA, timestamp+30)
#[1] 946724460 946724490 946724520 NA
Notice that still, timestamp+30
works as expected but lets say I want to replace NA dates by a fixed date and translate all the others by 30 secs:
fixedDate = as.POSIXct(strptime("2000-01-01 12:00:00.000000", formatString))
ifelse(is.na(timestamp), fixedDate, timestamp+30)
#[1] 946724460 946724490 946724520 946724400
Question: whats wrong with this solution and why doesn't it work as expected?
Edit: the desired output is a vector of timestamps (not of integers) translated by 30 secs and the NA's being replaced by whatever...
Aucun commentaire:
Enregistrer un commentaire