Error while converting into Date format in R?

r

#1

I have a data set in which there is variable name Effective To Date which has a date in the format of a factor .So I have decided to convert into date format .But while converting it I am getting an error.

as.Date(z$Effective.To.Date)
Error in charToDate(x) : 
  character string is not in a standard unambiguous format

#2

try adding the format option in your as.Date call as follows-

as.Date(z$Effective.To.Date, format="%m/%d/%Y")


#3

as.Date(as.character(z$Effective.To.Date), format="%m/%d/%Y")

As mentioned by @chandnijoshi09 you need to mention the correct format
%m/%d/%Y

Can you give some sample dates


#4

Hi everybody, this doesn’t seem to work for me and I can’t explain it to myself. Even weirder still is the fact that it works for some of my divisions, but not for others. For example if I want to take a subset of the records in a df for january, it works, but for february, it doesn’t.

Data is in standard format="%y-%m-%d":

    [963] "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01"
 [976] "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01"
 [989] "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01" "2017-01-01"

but when do:

feb <- subset(timedimgs[as.Date(timedimgs$mdate, format="%y-%m-%d") >= as.Date("2017-02-01") & as.Date(timedimgs$mdate, format="%y-%m-%d") <= as.Date("2017-02-31"), ])

It throws an the dreaded error.

Any ideas? I’m really baffled why it works for some months, but not for others.

P.S. the df I am subseting has values for the whole year, the example given above is just an outtake.


#5

Wait, I’m dumb as a nail; not all months have 31 days (DUH!). Sorry for this everybody!