i may be missing something really obvious here, could you help me?
when i send a date object to seLocalize() function, it returns me as expected for the code that follows (i sent 14-mar-18 and returned 13-mar-18)
seLocalize <- function(dataTeste) {
data <- as.POSIXlt(dataTeste)
if(data$wday != 0) {
data <- data-86400
}
print(data)
}
Although, for following the piece of code, the same test returns "Error: $ operator is invalid for atomic vectors"
seLocalize <- function(dataTeste) {
data <- as.POSIXlt(dataTeste)
while(data$wday != 0) {
data <- data-86400
}
print(data)
}
Why?
I'm having troubles believing this is a proper problem description since that date was in a format that would not be recognized.
> seLocalize <- function(dataTeste) {
+ data <- as.POSIXlt(dataTeste)
+ while(data$wday != 0) {
+ data <- data-86400
+ }
+ print(data)
+ }
> seLocalize("13-mar-18")
Error in as.POSIXlt.character(dataTeste) :
character string is not in a standard unambiguous format
So I tried sending what I thought was a possible revised function (for the somewhat obscure coercion induced error ) a proper date value for as.POSIXlt
:
seLocalize2 <- function(dataTeste) {
data <- as.POSIXlt(dataTeste)
while(data$wday != 0) {
data <- as.POSIXlt(data-86400) # coerce back to POSIXlt
}
print(data)
}
> seLocalize2("2018-03-25")
[1] "2018-03-25 PDT"
> seLocalize2("2018-03-29")
[1] "2018-03-25 PDT"