This question already has answers here:
converting datetime string to POSIXct date/time format in R
(1 answer)
How to convert string "MMM DD YYYY" to date YYYY-MM-DD
(1 answer)
Closed 7 months ago.
I have a character string date time string but need to convert the same into YYYYMM date format. Cannot seem to find a solution as all functions are converting into NA or weird date format.
Date_format_current <- '02/09/2020 23:35'
You can use the following code
library(lubridate)
library(zoo)
current <- '02/09/2020 23:35'
as.yearmon(dmy_hm(current))
#> [1] "Sep 2020"
#Or
format(dmy_hm(current), "%Y-%m")
#> [1] "2020-09"
#Or
format_ISO8601(dmy_hm(current), precision = "ym")
#> [1] "2020-09"
Related
This question already has answers here:
How to convert dd/mm/yy to yyyy-mm-dd in R
(6 answers)
Closed 3 years ago.
When I parse a character string into a date, Why does this not throw an error or an NA? I have tried the following
t <- "31-Oct-2012"
as.Date(t, format = "%d-%B-%Y") # this produces the expected result
as.Date(t, format = "%d-%B-%y") # I was expecting an NA
Instead I get
[1] "2020-10-31"
Because %y is for two digit year, so it takes only first two digits and ignores the rest. It treats t as
as.Date("31-Oct-20", format = "%d-%B-%y")
#[1] "2020-10-31"
This also works when you have anything after 2-digit year. See
as.Date("31-Oct-20ABC", format = "%d-%B-%y")
#[1] "2020-10-31"
R tries to "auto-complete" when there is less information, it returns some (incorrect) date for
as.Date("31-Oct-20", format = "%d-%B-%Y")
#[1] "0020-10-31"
but returns NA for
as.Date("31-Oct-ABC20", format = "%d-%B-%y")
#[1] NA
This question already has answers here:
integer data frame to date in R [duplicate]
(3 answers)
Closed 4 years ago.
I have a date.frame and a column with
values(20180213190133, 20180213190136, 20180213190173 , 20180213190193 , 20180213190213, 20180213190233, 20180213190333, 20180213190533, 20180213190733, 20180213190833, 20180213190833, 20180213190833, 201802131901833, 20180213191133, 20180213192133, 20180213194133, 20180213199133, 20180213199133, 20180213199133, 20180213199133, 20180213190136.... 1200 entries)
I want to convert this column which is of type int to Date.
I tried using :
as.Date() and as.POSIXct(). Both doesn't work. I am getting N/A value.
Please let me how can I convert this filed from int to Date.
Thanks
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
This question already has an answer here:
Convert R character to date [duplicate]
(1 answer)
Closed 4 years ago.
I have a csv file (df) with dates as " Mar-97, Apr-97..." . After importing to r with read.csv and stringAsFactors = F, the class(dates) is character.
I have tried : df$dates <- as.Date(df$Dates , format = "%d-%b-%y") & as.Date(df$Dates , format = "%b-%y"). class is converted to Date but it shows NA values?
you can try lubridate library:
library(lubridate)
> parse_date_time("Mar-97", "m y")
[1] "1997-03-01 UTC"
and you can vectorize
df=c("Mar 17","Apr 17")
> parse_date_time(df, "m y")
[1] "2017-03-01 UTC" "2017-04-01 UTC"
This question already has answers here:
Transform year/week to date object
(2 answers)
Closed 4 years ago.
In R, how do I convert the string 1/2010 (week 1 of 2010) into a Date or POSIXct (or POSIXlt) object?
I tried
as.Date('1/2010', "%W/%Y")
[1] "2010-06-29"
I also tried
strptime('1/2010', "%W/%Y")
[1] "2010-06-29 BRT"
But these are clearly not what I want.
In the end, I guess doesn't really matter which exact is picked, so long as I can correctly re-convert this to "weeks since origin".
library(splitstackshape)
date <- c("1/2013","3/2013")
date = data.frame(date)
df = data.frame(cSplit(date,"date","/"))
colnames(df) = c("week", "year")
df$date = as.Date(paste(df$year, df$week, 1, sep="-"), "%Y-%U-%u")
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 6 years ago.
I have a data vector that looks like this:
dates<-c("2014-11", "2014-12", "2015-01", "2015-02", "2015-03", "2015-04")
I am trying to convert it into a recognizable date format, however no luck:
as.Date(dates,"%Y-%m")
[1] NA NA NA NA NA NA
I suspect that the problem lies in that that there is no day specified.
Any thoughs of how this can be solved?
If we need to convert to Date class, it needs a day. So, we can paste with one of the days of interest, say 1, and use as.Date
as.Date(paste0(dates, "-01"))
The zoo package has a nice interface to this, which allows storing of year-month data and a as.Date method to coerce to a Date object. For example:
library("zoo")
dates <- c("2014-11", "2014-12", "2015-01", "2015-02", "2015-03", "2015-04")
The function to convert the character vector or year-months into a yearmon is as.yearmon. The second argument is the format of the date parts in the individual strings. Here I use
%Y for year with century
%m for the month as a decimal
Separated by literal -
.
yrmo <- as.yearmon(dates, "%Y-%m")
This gives
> yrmo
[1] "Nov 2014" "Dec 2014" "Jan 2015" "Feb 2015" "Mar 2015" "Apr 2015"
This is actually the default, so you can leave off the format part entirely, e.g. yrmo <- as.yearmon(dates)
To convert to a Date class object, the as.Date method is used
> as.Date(yrmo)
[1] "2014-11-01" "2014-12-01" "2015-01-01" "2015-02-01" "2015-03-01"
[6] "2015-04-01"
This method has a second argument frac which is specified allows you to state how far through the month you want each resulting Date element to be (how many days as a fraction of the length of the month in days)
> as.Date(yrmo, frac = 0.5)
[1] "2014-11-15" "2014-12-16" "2015-01-16" "2015-02-14" "2015-03-16"
[6] "2015-04-15"