Commands as. Date and srtptime in R

Asked

Viewed 656 times

3

Suppose the following vector:

datas=c("26/12/2014", "27/12/2014", "28/12/2014", "29/12/2014", "30/12/2014", "31/12/2014")

When applying the commands as.Date(datas, "%d/%m%y") and strptimes(datas, "%d/%m%y") the exit is:

"2020-12-26" "2020-12-27" "2020-12-28" "2020-12-29" "2020-12-30" "2020-12-31"

Why is the Year (y) wrong? How do I adjust this?

3 answers

2

The function dmy of lubridate can interpret dates considering what they see as (day, month, year):

library(lubridate)
dmy(datas)
# [1] "2014-12-26 UTC" "2014-12-27 UTC" "2014-12-28 UTC" "2014-12-29 UTC" "2014-12-30 UTC"
# [6] "2014-12-31 UTC"

the same goes for ymd and other variations.

2


You need to use the command:

as.Date(datas, "%d/%m/%Y")

Note the %Y uppercase. It indicates that the year is in four digits. When you use the %y tiny, he understands that the year has only two digits and so puts 2020.

2

You should use uppercase Y to indicate that the year format has 4 digits.

datas=c("26/12/2014", "27/12/2014", "28/12/2014", "29/12/2014", "30/12/2014", "31/12/2014")

as.Date(datas, "%d/%m/%Y") 
#[1] "2014-12-26" "2014-12-27" "2014-12-28" 
#[4] "2014-12-29" "2014-12-30" "2014-12-31"

strptime(datas, "%d/%m/%Y") 
#[1] "2014-12-26 BRST" "2014-12-27 BRST" "2014-12-28 BRST" 
#[4] "2014-12-29 BRST" "2014-12-30 BRST" "2014-12-31 BRST"
  • Understood. When using strptime what means the "BRST"?

  • BRST - Brasília Summer Time (Time Zone Abbreviation)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.