There are many ways how you work out with dates in R.
For exemple,
suppose you have a date frame as follow
##creating a data frame
dates <- c("2018/01/25","2017/04/14","2012/12/24","2018/05/15","2058/02/25")
index <- 1:5
df <- data.frame(dates=dates, index=index, stringsAsFactors = F)
Checking the struct of the date we obtain
##checking the type of the dates
str(df)
'data.frame': 5 obs. of 2 variables:
$ dates: chr "2018/01/25" "2017/04/14" "2012/12/24" "2018/05/15" ...
$ index: int 1 2 3 4 5
So we want convert the character column of our data frame (df) to a date format
##converting data type in R
df$dates <- as.Date(df$dates, "%Y/%m/%d")
#checking the new struct of the data frame
str(df)
'data.frame': 5 obs. of 2 variables:
$ dates: Date, format: "2018-01-25" "2017-04-14" "2012-12-24" "2018-05-15" ...
$ index: int 1 2 3 4 5
You could also use a package called lubridate
that gives you more date options functions to work with.
For exemple
##other ways to work out
library(lubridate)
df$dates <- ymd(df$dates)
str(df)
data.frame': 5 obs. of 2 variables:
$ dates: Date, format: "2018-01-25" "2017-04-14" "2012-12-24" "2018-05-15" ...
$ index: int 1 2 3 4 5
Nevertheless, when we used those functions by default they changing the slash by hifen. We can change that using format
function
format(df$dates, "%Y/%m/%y")
"2018/01/18" "2017/04/17" "2012/12/12" "2018/05/18" "2058/02/58"
or whenever symbol we preferer
format(df$dates, "%Y&%m&%y")
"2018&01&18" "2017&04&17" "2012&12&12" "2018&05&18" "2058&02&58"