Unable to format months using as.Date

I am missing something obvious in the "format" as.Date section. Consider this example

d1 <- data.frame(d = c("1/Jan/1947", "1/Feb/1947", "1/Mar/1947"), d2 = c("Jan/1947", "Feb/1947", "Mar/1947"))

 d1$date1 <- as.Date(x=d1$d, format="%d/%b/%Y") d1$date2 <- as.Date(x=d1$d2, format="%b/%Y") d d2 date1 date2 1 1/Jan/1947 Jan/1947 1947-01-01 <NA> 2 1/Feb/1947 Feb/1947 1947-02-01 <NA> 3 1/Mar/1947 Mar/1947 1947-03-01 <NA> 

so my question is very simple - I do not understand why date1 works, but date2 does not.

+6
source share
4 answers

The simplest answer is that a date is something that includes a day, and if not specified, as.Date () gets confused. From the documentation? As.Date:

If the date string does not completely indicate the date, the response may be systemic. Most Common Behavior The missing year, month, or day is assumed to be current. If it sets the date incorrectly, reliable implementations enter an error, and the date will be reported as "NA. Unfortunately, some common implementations (for example," glibc ") are unreliable and guess at the expected value.

When you think about it, the term "Mar / 1947" is not, strictly speaking, a date - it is just a combination of a month and a year. A date is a specific day in March 1947 (or any other month + year), since you do not specify it, you do not have a date.

+16
source

This is because d2 in your data.frame is an data.frame date. It does not contain a day. To get around this, consider the following:

 d1$date2 <- as.Date(x=paste("1/",d1$d2, sep=""), format="%d/%b/%Y") > d1 d d2 date1 date2 1 1/Jan/1947 Jan/1947 1947-01-01 1947-01-01 2 1/Feb/1947 Feb/1947 1947-02-01 1947-02-01 3 1/Mar/1947 Mar/1947 1947-03-01 1947-03-01 
+9
source

I do not know, but% b does not work when it is the leading field.

Listed below are all failures (give NA):

 > as.Date("Jan/1947", format="%b/%Y") > as.Date("Jan 1947", format="%b %Y") > as.Date("jan1947", format="%b%Y") > as.Date("Jan1947", format="%b%Y") 

then when you precede% b with% d, it works:

 > as.Date("1Jan1947", format="%d%b%Y") > as.Date("29-Jan-1947", format="%d-%b-%Y") > as.Date("08/Aug/1947", format="%d/%b/%Y") > as.Date("22 Dec 1947", format="%d %b %Y") 

Neilfws seems to have an incomplete answer. This also explains why giving only a year gives:

 > as.Date("1947", format="%Y") [1] "1947-09-19" 
0
source

According to Cole Beck's Date-Time Processing in R document, inside the date is stored as a single numerical value that counts the number of days that have passed since the key date, 1970-01-01. Example: 1970-01-31 will be saved internally as 30.

So, returning to the problem when the day (% d) is not mentioned in the given input date (i.e., Incomplete date), it cannot store the date inside, which leads to the โ€œWarning message: NA entered by forceโ€

Source: http://biostat.mc.vanderbilt.edu/wiki/pub/Main/ColeBeck/datestimes.pdf

0
source

Source: https://habr.com/ru/post/897656/


All Articles