I have loaded a txt dataset into R. A part of it can be seen below:
date rf
1 19620102 0.011
2 19620103 0.011
3 19620104 0.011
4 19620105 0.011
5 19620108 0.011
I have tried to convert the dates using the following code:
df$date <- format(as.Date(df$date), "%Y-%m-%d")
This gives the following results, that are not what I wanted:
date rf
1 55688-01-06 0.011
2 55688-01-07 0.011
3 55688-01-08 0.011
4 55688-01-09 0.011
5 55688-01-12 0.011
I wanted to convert the dates into the format as seen below:
date rf
1 1962-01-02 0.011
2 1962-01-03 0.011
3 1962-01-04 0.011
4 1962-01-05 0.011
5 1962-01-08 0.011
Does anyone know how I can get the results that I want?
This works
as.Date("19620102", "%Y%m%d")
#> [1] "1962-01-02"
Created on 2021-02-06 by the reprex package (v0.3.0)
So just do
as.Date(as.character(df$date), "%Y%m%d")
We could also use ymd
from lubridate
library(lubridate)
ymd(19620102)
#[1] "1962-01-02"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.