简体   繁体   中英

Trouble converting numbers to dates in R

I am trying to convert the following numbers to dates in R:

在此处输入图片说明

Use as.Date with the correct format mask:

dates <- c(19801231, 19810130, 19810227)
as.Date(as.character(dates), format="%Y%m%d")

[1] "1980-12-31" "1981-01-30" "1981-02-27"

Note that if your Names.Date column already be text, then you can skip the extra conversion and just use:

as.Date(dates, format="%Y%m%d")

You can use lubridate package.

> library(lubridate)
> Names.Date <- c(19810130, 19810227, 19810331)
> Names.Date
[1] 19810130 19810227 19810331
> ymd(Names.Date)
[1] "1981-01-30" "1981-02-27" "1981-03-31"
> 

An option with anydate

library(anytime)
anydate(dates)
#[1] "1980-12-31" "1981-01-30" "1981-02-27"

data

dates <- c(19801231, 19810130, 19810227)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM