Integer Data Frame to Date in R

integer data frame to date in R

You have to reference specific columns rather than just referencing the data frame. If the variable containing the integer dates is in a data frame df and is called x, you can do this:

df <- transform(df, x = as.Date(as.character(x), "%Y%m%d"))

# x
# 1 1982-05-09
# 2 1955-05-03
# 3 2008-05-05
# 4 1959-05-05
# 5 1994-05-17
# 6 1969-05-04
# 7 2005-04-20
# 8 2006-05-03
# 9 1984-04-27
# 10 1955-05-13

This converts the integers to character strings, then interprets the strings as dates.

If you multiple columns containing dates in this format, you can convert them in one fell swoop, but you have to do it slightly differently:

df <- data.frame(lapply(df, function(x) as.Date(as.character(x), "%Y%m%d")))

Or even better, as docendo discimus mentioned in a comment:

df[] <- lapply(df, function(x) as.Date(as.character(x), "%Y%m%d"))

How to convert integer to date format in R?

If you have

x <- c(200502, 200503)

Then

as.Date(x, origin = "2000-01-01")

tells R you want the days 200,502 and 200,503 days after 2000-01-01. From help("as.Date"):

as.Date will accept numeric data (the number of days since an epoch),
but only if origin is supplied.

So, integer data gives days after the origin supplied, not some sort of numeric code for the dates like 200502 for "2005-02-01".

What you want is

as.Date(paste(substr(x, 1, 4), substr(x, 5, 6), "01", sep = "-"))

# [1] "2005-02-01" "2005-03-01"

The

paste(substr(x, 1, 4), substr(x, 5, 6), "01", sep = "-")

part takes your integers and creates strings like

# [1] "2005-02-01" "2005-03-01"

Then as.Date() knows how to deal with them.

You could alternatively do something like

as.Date(paste0(x, "01"), format = "%Y%m%d")

# [1] "2005-02-01" "2005-03-01"

This just pastes on an "01" to each element (for the day), converts to character, and tells as.Date() what format to read the date into. (See help("as.Date") and help("strptime")).

Converting Integers to Date in R

Use the format option when calling as.Date:

dates <- as.character(data$YearMonthDay)
data$newVar = as.Date(dates, format="%Y%m%d")

Note that you don't need to use the origin parameter here, because even though you are passing in numerical data, it is in the form of an actual text date. origin is used when passing in number of days since an epoch.

so I want to format the date to include only the month and day

It is generally not advisable to go in this direction, and in any case, base R date's have to have a year component. So, include the year component, and if you don't want to use it, then don't use it.

Converting integer to date in R and calculating no. of days in between?

Here's how you'd do it using as.Date

as.Date("20111113", format = "%Y%m%d")
#[1] "2011-11-13"

as.Date("20111113", format = "%Y%m%d") - as.Date("20120420", format = "%Y%m%d")
#Time difference of -159 days

You can also use anydate command of anytime package to parse integer into date and obtain the difference by subtracting.

library(anytime)

anydate(20111113)
#[1] "2011-11-13"

anydate(20111113) - anydate(20120420)
#Time difference of -159 days

How do you convert an integer into a date (format YYYY) in R

As suggested by @caldwellst in the comments you can just pull the first 4 characters if they're all in the format you indicated. Then you can use as.Date(format = "%Y) to turn into actual date format and just fill in the M and D portions. Then use lubridate::year() to pull the year info.

library(tidyverse)
library(lubridate)

c(17701216, 177012, 1770) %>%
as.character() %>%
str_sub(1, 4) %>%
as.Date(format = "%Y") %>%
year()
#> [1] 1770 1770 1770

Created on 2022-02-10 by the reprex package (v2.0.1)

R: How to store date in a data.frame and prevent R from converting it to numbers?

Make date_of_entry column of Date class before assigning the value (currently it is of class "logical").

mydat$date_of_entry <- as.Date(mydat$date_of_entry)
mydat$date_of_entry[mydat$id == 1] <- mydate
mydat

# id date_of_entry
#1 1 2017-01-23
#2 1 2017-01-23
#3 2 <NA>

Also, those numbers are numeric representation of dates so you can also convert them to dates from numbers.

#dataframe
mydat <- data.frame(id = c(1, 1, 2), date_of_entry = c(NA, NA, NA))
#date converted to number
mydat$date_of_entry[mydat$id == 1] <- mydate
#convert it back to date
mydat$date_of_entry <- as.Date(mydat$date_of_entry)

R - Numeric(int) to Date conversion

Try this:

Input data

values<-c(20180213190133, 20180213190136, 20180213190173)

values_date<-as.Date(substr(as.character(values),start = 1,stop=8), format = "%Y%m%d")
> values_date
[1] "2018-02-13" "2018-02-13" "2018-02-13"
> class(values_date)
[1] "Date"

If you want to mantain also hour/minute/second you can try this:

values_date<-as.POSIXlt(as.character(values), format = "%Y%m%d%H%M%S")

After this the class will be "POSIXlt" "POSIXt" and not Date but there are some strange info in your input data

In the third number, last two figures are "73", this number is incorrect for seconds and you will have NA in output.

values_date
[1] "2018-02-13 19:01:33 CET" "2018-02-13 19:01:36 CET" NA

Convert column in data.frame to date

Do the transformations within mutate

df2 %>%
group_by(a1) %>%
mutate(b2=as.Date(b2, format = "%d.%m.%Y"))
# a1 b2 c3 d3
# (chr) (date) (chr) (int)
#1 a 2015-01-01 1a 1
#2 a 2015-02-02 2b 2
#3 b 2012-02-14 3c 3
#4 b 2008-08-16 4d 4
#5 c 2003-06-17 5e 5
#6 d 2015-01-31 6f 6
#7 e 2022-01-07 7g 7
#8 e 2001-05-09 8h 8

If we need to do only the transformation, we don't need to group by 'a1'.

mutate(df2, b2= as.Date(b2, format= "%d.%m.%Y"))

By using %<>% operator from magrittr, we can transform in place.

df2 %<>%
mutate(b2= as.Date(b2, format= "%d.%m.%Y"))


How to convert time series dates into data frame dates

ts series do not understand Date class but you can encode the dates into numbers and then decode them back. Assuming that you want a series with frequency 52 the first week in 2016 will be represented by 2016, the second by 2016+1/52, ..., the last by 2016+51/52.

For example,

tt <- ts(rnorm(305), start = 2016, freq = 52)

Now decode the dates.

toDate <- function(tt) {
yr <- as.integer(time(tt))
week <- as.integer(cycle(tt)) # first week of year is 1, etc.
as.Date(ISOdate(yr, 1, 1)) + 7 * (week - 1)
}

data.frame(dates = toDate(tt), series = c(tt))

We can also convert from Date class to year/week number

# input is a Date class object
to_yw <- function(date) {
yr <- as.numeric(format(date, "%Y"))
yday <- as.POSIXlt(date)$yday # jan 1st is 0
week <- pmin(floor(yday / 7), 51) + 1 # 1st week of yr is 1
yw <- yr + (week - 1) / 52
list(yw = yw, year = yr, yday = yday, week = week)
}


Related Topics



Leave a reply



Submit