Warm tip: This article is reproduced from serverfault.com, please click

Parsing dates in R with weird format

发布于 2020-11-28 00:30:37

Hi guys I have a list of dates with this weird format : X1.22.20 X1.23.20 (month/day/year). and I would like to have "2020-06-11" ('%d %b %Y'). I tried this:

> min.date <- min(dates)
> max.date <- max(dates)
> min.date.txt <- min.date %>% format('%d %b %Y')
> max.date.txt <- max.date %>% format('%d %b %Y') %>% paste('UTC')
> min.date
[1] "2002-10-10" 

And the value is crazy because I know for sure that there are not 2002 int his data. Any help? Thanks

Questioner
Paulo Sergio Schlogl
Viewed
0
G. Grothendieck 2020-11-28 09:01:03

Assuming that the question is how to convert the input x shown below to Date class use as.Date with a format that corresponds to the input so it must start with X, have dots where the input has dots, etc. Look at ?strptime for documentation on the percent codes.

x <- c("X1.22.20", "X1.23.20") # input

as.Date(x, format = "X%m.%d.%y")
## [1] "2020-01-22" "2020-01-23"

Note that if you got those dates like this:

Lines <- "1.22.20 1.23.20
1 2
3 4"
read.table(text = Lines, header = TRUE)
##   X1.22.20 X1.23.20
## 1        1        2
## 2        3        4

then the X can be avoided using check.names = FALSE as follows:

read.table(text = Lines, header = TRUE, check.names = FALSE)
##   1.22.20 1.23.20
## 1       1       2
## 2       3       4