convert numeric column to dates recognized by R - r

How could I convert this numeric vector of dates into the date format that R can recognize?
date <- c(29101958L, 10121957L, 27091953L, 23021960L,
6031967L, 10011968L, 10101958L, 9101992)
I would like an output like:
'1958-10-29', '1957-12-10', '1953-09-27', '1960-02-23', '1967-03-06', '1968-01-10', '1958-10-10', '1992-10-09'
Then I would like to calculate the age by making the difference from 2016-12-31 with the dates of the vector.
I appreciate any help.

We can use dmy from lubridate
library(lubridate)
newdate <- dmy(date)
newdate
#[1] "1958-10-29" "1957-12-10" "1953-09-27" "1960-02-23" "1967-03-06" "1968-01-10" "1958-10-10" "1992-10-09"
and get the difference between the new date in years
as.integer(difftime(as.Date('2016-12-31'), newdate, units = 'days')/365)
#[1] 58 59 63 56 49 49 58 24

Base R option using as.Date :
as.Date(sprintf('%08d', date), '%d%m%Y')
#[1] "1958-10-29" "1957-12-10" "1953-09-27" "1960-02-23" "1967-03-06"
#[6] "1968-01-10" "1958-10-10" "1992-10-09"
Using sprintf we add leading zeroes for single digit dates.

Related

Converting a date in R returns NA

date
05-06-2016
05-07-2016
4/13/2016
4/14/2016
I want to format the column to date format using below code
td3 <- read.csv("Book2.csv")
td3$date <- as.Date(td3$date, "%m-%d-%y")
when i run the code the last 2 rows return NA
as.Date.character(gsub("/", "-",td3$date), '%m-%d-%Y')
[1] "2016-05-06" "2016-05-07" "2016-04-13" "2016-04-14"
Here is a solution with parse_date_time from lubridate package:
library(lubridate)
as.Date(parse_date_time(df$date, orders = c('mdy', 'dmy')))
[1] "2016-05-06" "2016-05-07" "2016-04-13" "2016-04-14"

Convert a string into dates using R

I have a column of dates written as monthyear in the format:
11960 - this would be Jan 1960
121960 - this would be Dec 1960
I would like to convert this column into a day-month-year format assuming the first of the month as each date.
I have tried (using one number as an example as opposed to dt$dob)
x <- sprintf("%08d%", 11960)
and then x <- as.date(x, format = "%d%m%Y)
but this gives me NAs as I assume it doesn't like the 00 at the start
So I tried pasting 01 to each value but this pastes it to the end (R noob here). I was thinking maybe posting 01 to the start and then using the sprintf function may work still:
paste 01 to start of 11960 = 011960
sprintf("%08d%", 011960) to maybe give 0101960?
Then use as.Date to convert?
Many thanks for your help
i used paste0() instead of sprintf, but it seems it works.
> x<-paste0("010",11960)
> x
[1] "01011960"
> as.Date(x , format = "%d%m%Y" )
[1] "1960-01-01"
EDIT for 2 digit months i use ifelse() and nchar()
y<-c(11960,11970,11980, 111960,111970,111980)
x<-ifelse(nchar(y) == 5,paste0("010",y),paste0("01",y))
> x
[1] "01011960" "01011970" "01011980" "01111960" "01111970" "01111980"
as.Date(x , format = "%d%m%Y" )
[1] "1960-01-01" "1970-01-01" "1980-01-01" "1960-11-01" "1970-11-01" "1980-11-01"

How to convert numeric values to time without the date?

I want convert numeric values to time without the date for the data like 1215,1423,1544,1100,0645,1324 in R.
These data has to read like 12:15,14:23,15:44.
I was trying as.POSIXct.
We can use strptime with format
format(strptime(sprintf("%04d", v1), "%H%M"), "%H:%M")
The above output is character class, but if we needed a times class, then we can use times from chron on a "HH:MM:SS" format created with sub or from the above code
library(chron)
times(sub("(.{2})(.{2})","\\1:\\2:", sprintf("%04d00", v1)))
#[1] 12:15:00 14:23:00 15:44:00 11:00:00 06:45:00 13:24:00
Or
times(format(strptime(sprintf("%04d", v1), "%H%M"), "%H:%M:%S"))
data
v1 <- c( 1215,1423,1544,1100,0645,1324)

replace date in data.table in R returns unexpected value

I have one date variable that I formatted in the following way:
date <- as.POSIXct(date, "%m-%d-%Y-%X")
For example, this can be the most recent date:
"2014-03-04 23:59:59 EST"
Now I have a data.table DT, in which a column time indicates some other date and is also formated as.POSIXct(format: "%m-%d-%Y-%X"). Now I want to replace some missing values (NA) in DT[,time] with my date variable "date":
library(data.table)
DT <- DT[is.na(time), time:= date]
However, the dates that were replaced in the data.table are now "1970-01-01 14:30:24" (and not "2014-03-04 23:59:59").
What am I missing?
R: 3.02
Data.table: 1.9.2
The problem is not data.table but essentially because you try to mix "Datetime" types with an another type in the same vector. This reproduce the error:
library(lubridate) ## I am using lubridate for smart date conversion
origin <- mdy_hms("01-01-1970-00:00:01") ## Using origin as default value for dates
date <- mdy_hms("3-11-2014-09:12:30")
time = c(NA,1)
ifelse(is.na(time),date,origin)
[1] 1394529150 1 ## date is converted to numeric
one solution is to convert first to string and then convert again to a datetime
ymd_hms(ifelse(is.na(time),paste(date),paste(origin))) ## paste used as as.character
using data.table , you can get the same result :
dt = data.table(time=time,date = date)
dt[,time:=ymd_hms(ifelse(is.na(time),paste(date),
paste(origin)))]
time date
1: 2014-03-11 09:12:30 2014-03-11 09:12:30
2: 1970-01-01 00:00:01 2014-03-11 09:12:30
PS: better to not coerce time variable here and do operations whenever you have a missing values.

Find month from week numbers using lubridate

I have this list of dates:
library(lubridate)
my.dates = ymd(c("2013-12-14", "2014-01-18", "2014-01-27", "2013-12-13", "2013-12-29", "2013-12-06"))
The following lubridate::weekfunctions outputs a numeric vector when I convert these dates to week numbers:
week(my.dates)
[1] 50 3 4 50 52 49
Can I get lubridate to output a date ("POSIXct" "POSIXt") object that converts my.dates to a week number and year number. So output should be a date object (not a character or numeric vector) formatted something like this:
[1] "50-2013" "3-2014" "4-2014" "50-2013" "52-2013" "49-2013"
I'm specifically interested in a solution that uses lubridate.
To convert my.dates to a week-year character vector try the following where week and year are lubridate functions:
> paste(week(my.dates), year(my.dates), sep = "-")
[1] "50-2013" "3-2014" "4-2014" "50-2013" "52-2013" "49-2013"
The sample output in the question did not use leading zeros for the week but if leading zeros were desired for the week then:
> sprintf("%02d-%d", week(my.dates), year(my.dates))
[1] "50-2013" "03-2014" "04-2014" "50-2013" "52-2013" "49-2013"
The above are character representations of week-year and do not uniquely identify a date nor can such a format represent a POSIXt object.

Resources