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"))
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.
Convert integer to class Date
as.character()
would be the general way rather than use paste()
for its side effect
> v <- 20081101
> date <- as.Date(as.character(v), format = "%Y%m%d")
> date
[1] "2008-11-01"
(I presume this is a simple example and something like this:
v <- "20081101"
isn't possible?)
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")
).
error in function for converting integer to date format in r, error: do not know how to convert '.' to class “Date”
Can't tell you exactly, why the function worked in the first place. However, your function is in my opinion not well specfied or messed up. If you want to use tidy eval then have a look at my function date_format_new
. However, in my opinion there is a simpler way to set up the function, which I coded as date_format_simple
.
library(dplyr)
df <- data.frame(
column_character = "201802171254",
column_integer64 = 201802171254,
stringsAsFactors = FALSE
)
date_format <- function(df, col){
col_q = enquo(col)
df %>%
base::as.Date(as.character(c(!!col_q), na.rm = TRUE), format = '%Y%m%d%H%M')
}
date_format(df$column_character)
#> [1] "2018-02-17"
date_format(df$column_integer64)
#> Error: Quosures can only be unquoted within a quasiquotation context.
#>
#> # Bad:
#> list(!!myquosure)
#>
#> # Good:
#> dplyr::mutate(data, !!myquosure)
# Works
date_format_new <- function(df, col){
col_q = enquo(col)
df %>%
pull(!!col_q) %>%
as.character() %>%
base::as.Date(format = '%Y%m%d%H%M')
}
date_format_new(df, column_character)
#> [1] "2018-02-17"
date_format_new(df, column_integer64)
#> [1] "2018-02-17"
# Simpler
date_format_simple <- function(col){
base::as.Date(as.character(col), format = '%Y%m%d%H%M')
}
date_format_simple(df$column_character)
#> [1] "2018-02-17"
date_format_simple(df$column_integer64)
#> [1] "2018-02-17"
Created on 2020-04-09 by the reprex package (v0.3.0)
Related Topics
Applying a Function to Every Row of a Table Using Dplyr
Yaml Current Date in Rmarkdown
R Shiny Passing Reactive to Selectinput Choices
Call Apply-Like Function on Each Row of Dataframe With Multiple Arguments from Each Row
Create a Co-Occurrence Matrix from Dummy-Coded Observations
Data.Table "Key Indices" or "Group Counter"
Finding Percentage in a Sub-Group Using Group_By and Summarise
How to Swap Values Between Two Columns
How to Center Stacked Percent Barchart Labels
Intelligent Point Label Placement in R
Read All Files in a Folder and Apply a Function to Each Data Frame
All Levels of a Factor in a Model Matrix in R
Ignore Outliers in Ggplot2 Boxplot
Rcpp Pass by Reference Vs. by Value