How to convert a numeric value into a Date value - r

So, I have a data.frame with a column called Date.birth, but I have these values in a numeric format:
Date.birth
43067
43060
Probably is problem format. But I need in a Date format like these:
Date.birth
11/28/17
11/21/17
Actually the above format is the correct. I tried this command:
as.Date(levels(data$Date.birth), format="%d.%m.%Y")
but didn't work. Anyone has a suggestion?
Thanks.

We need to specify the origin if it is a numeric value
as.Date(data$Date.birth, origin = "1899-12-30")
e.g.
as.Date(43067, origin = "1899-12-30")
#[1] "2017-11-28"
After converting to Date class, if it needs to be in a custom format, use format
format(as.Date(43067, origin = "1899-12-30"), "%m/%d/%y")
#[1] "11/28/17"
If your column is factor, do convert to numeric first
as.Date(as.numeric(as.character(data$Date.birth)), origin = "1899-12-30")

If this is an excel numeric date, janitor has a great solution:
library(janitor)
excel_numeric_to_date(data$Date.birth)

It can be simply done by using lubridate package-
lubridate::as_date(as.numeric(dt$Date.birth),origin="1899-12-30")
[1] "2017-11-28" "2017-11-21"
Sample Data-
dt <- read.table(text="Date.birth
43067
43060",header=T)

try this
VariableName <- dt %>%
mutate(Date.birth = format(excel_numeric_to_date(as.numeric(Date.birth)),"%m/%d/%y"))
#[1] "11/28/17"

Related

Convert a character column to dates in R

I am trying to convert a data column (x_date) that has this form "31.03.2013" (the class is "character") into Dates in the form of "2013-01-31"
I tried with the following codes:
as.Date(x_date, format = "%d-%m-%Y")
as.Date(x_date, format="%Y-%m-%d")
as.Date(x_date,format= "%Y-%m-%d", tryFormats = c("%Y-%m-%d", "%Y/%m/%d", "%d.%m.%Y"), optional=FALSE )
in all of the three cases the complete data column turns into "NA".
Then I tried this code:
format.Date(x_date, format="%Y-%m-%d")
and I get an error warning.
Can anybody help me to convert my column into the respective Dates?
Specify the format of the data instead of in tryFormats
as.Date(x_date, format = '%d.%m.%Y')

Convert number that is in factor type to date [duplicate]

So, I have a data.frame with a column called Date.birth, but I have these values in a numeric format:
Date.birth
43067
43060
Probably is problem format. But I need in a Date format like these:
Date.birth
11/28/17
11/21/17
Actually the above format is the correct. I tried this command:
as.Date(levels(data$Date.birth), format="%d.%m.%Y")
but didn't work. Anyone has a suggestion?
Thanks.
We need to specify the origin if it is a numeric value
as.Date(data$Date.birth, origin = "1899-12-30")
e.g.
as.Date(43067, origin = "1899-12-30")
#[1] "2017-11-28"
After converting to Date class, if it needs to be in a custom format, use format
format(as.Date(43067, origin = "1899-12-30"), "%m/%d/%y")
#[1] "11/28/17"
If your column is factor, do convert to numeric first
as.Date(as.numeric(as.character(data$Date.birth)), origin = "1899-12-30")
If this is an excel numeric date, janitor has a great solution:
library(janitor)
excel_numeric_to_date(data$Date.birth)
It can be simply done by using lubridate package-
lubridate::as_date(as.numeric(dt$Date.birth),origin="1899-12-30")
[1] "2017-11-28" "2017-11-21"
Sample Data-
dt <- read.table(text="Date.birth
43067
43060",header=T)
try this
VariableName <- dt %>%
mutate(Date.birth = format(excel_numeric_to_date(as.numeric(Date.birth)),"%m/%d/%y"))
#[1] "11/28/17"

Change factor into time format

A column contains only time in H:M:S(e.g. 13:08:20) but its in FACTOR format so now I want to change the FACTOR into POSIXct so that I can apply ceiling date() function on it.
I have tried these in some cases when I run its shows no error but then the columns whole contains NA values. :
x <- anytime(cam5$CaptureTime)
x <- hms(cam5$CaptureTime)
x <- hms(as.character(cam5$CaptureTime))
x <- as.POSIXct(cam5$CaptureTime)
x <- as.POSIXct(as.character(cam5$CaptureTime))
We can use as.POSIXct and specify the format
as.POSIXct("13:08:20", format = "%T")
Or specifying it separately
as.POSIXct("13:08:20", format = "%H:%M:%S")
This would also work with strptime
strptime("13:08:20", format = "%T")
We can use hms from lubridate
library(lubridate)
hms("13:08:20")

How can I reformat a series of dates in a vector in R

I have vector of dates that i'm trying to convert with as date but i'm not getting the expected output, when I sapply with as.Date instead of getting a series of reformatted dates I get the names as dates and some odd value.
dates = c("20-Mar-2015", "25-Jun-2015", "23-Sep-2015", "22-Dec-2015")
sapply(dates, as.Date, format = "%d-%b-%Y")
20-Mar-2015 25-Jun-2015 23-Sep-2015 22-Dec-2015
16514 16611 16701 16791
I would like each of the values in the vector to be showing the new formated value. E.g. like what would happen if as.Date was shown applied to each element
as.Date("20-Mar-2015", format = "%d-%b-%Y")
[1] "2015-03-20"
You can directly use as.Date(dates, format = "%d-%b-%Y"). as.Date is vectorized, i.e. it can take a vector as input, not only a single entry.
In your case:
dates <- c("20-Mar-2015", "25-Jun-2015", "23-Sep-2015", "22-Dec-2015")
as.Date(dates, format = "%d-%b-%Y")
# [1] "2015-03-20" "2015-06-25" "2015-09-23" "2015-12-22"

Create date column from datetime in R

I am new to R and I am an avid SAS programmer and am just having a difficult time wrapping my head around R.
Within a data frame I have a date time column formatted as a POSIXct with the following the column appearing as "2013-01-01 00:53:00". I would like to create a date column using a function that extracts the date and a column to extract the hour. In an ideal world I would like to be able to extract the date, year, day, month, time and hour all within the data frame to create these additional columns within the data frame.
It is wise to always to be careful with as.Date(as.POSIXct(...)):
E.g., for me in Australia:
df <- data.frame(dt=as.POSIXct("2013-01-01 00:53:00"))
df
# dt
#1 2013-01-01 00:53:00
as.Date(df$dt)
#[1] "2012-12-31"
You'll see that this is problematic as the dates don't match. You'll hit problems if your POSIXct object is not in the UTC timezone as as.Date defaults to tz="UTC" for this class. See here for more info: as.Date(as.POSIXct()) gives the wrong date?
To be safe you probably need to match your timezones:
as.Date(df$dt,tz=Sys.timezone()) #assuming you've just created df in the same session:
#[1] "2013-01-01"
Or safer option #1:
df <- data.frame(dt=as.POSIXct("2013-01-01 00:53:00",tz="UTC"))
as.Date(df$dt)
#[1] "2013-01-01"
Or safer option #2:
as.Date(df$dt,tz=attr(df$dt,"tzone"))
#[1] "2013-01-01"
Or alternatively use format to extract parts of the POSIXct object:
as.Date(format(df$dt,"%Y-%m-%d"))
#[1] "2013-01-01"
as.numeric(format(df$dt,"%Y"))
#[1] 2013
as.numeric(format(df$dt,"%m"))
#[1] 1
as.numeric(format(df$dt,"%d"))
#[1] 1
Use the lubridate package. For example, if df is a data.frame with a column dt of type POSIXct, then you could:
df$date = as.Date(as.POSIXct(df$dt, tz="UTC"))
df$year = year(df$dt)
df$month = month(df$dt)
df$day = day(df$dt)
# and so on...
If your can store your data in a data.table, then this is even easier:
df[, `:=`(date = as.Date(as.POSIXct(dt, tz="UTC")), year = year(dt), ...)]

Resources