This question already has an answer here:
R formatting a date from a character mmm dd, yyyy to class date [duplicate]
(1 answer)
Closed 5 years ago.
I have a csv file where the dates look like this: Jan 31, 2017
I would like it be 2017-01-31
I've been searching the site and I found a lot of similar questions but none with my strange date format.
EDIT: Was unclear. I need to change a lot of dates so doing it manually won't work. Thanks for suggestions, reading the help function of lubridate and strptime now.
Use the lubridate library:
library(lubridate)
date <- "Jan 31, 2017"
date2 <- mdy(date)
date2
[1] "2017-01-31"
Related
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 12 months ago.
This question has appeared before, I know, but I haven't been able to derive the correct output after running Sys.setlocale("LC_TIME", "american").
Ok, so I have dataframe with a column of dates that look something like "October 2020" and "June 2021", etc.
I run as.Date(dataframe$month_column, format = "%B %Y"), and I do this after running Sys.setlocale() with the above inputs. But my output continues to be a bunch of NAs.
I suppose one work around I could try would be to split the Month and Year parts by the space into two columns, which might make it easier to coerce into date types, but I'd like to avoid that if possible, since I want to plot quantities by Month Year.
Any insight would be appreciated.
You can convert character strings of month and years with the my() function in the lubridate package. my, in this case, stands for month-year (there are also other functions, like mdy() which look for entries in the form of month-day-year, and dym() which expect day-month-year order).
library(lubridate)
dates <- data.frame(
dt = c("October 2020", "June 2021")
)
dates$converted <-my(dates$dt)
dates
Output:
dt converted
1 October 2020 2020-10-01
2 June 2021 2021-06-01
This question already has answers here:
How to convert a numeric value into a Date value
(4 answers)
Closed 2 years ago.
Wondering if you can give me some light about an outstanding problem I have in R, version 4.0.2. I am moving data from an excel file in a given sharepoint to a local DB. The problem facing is that when reading the file using readxl the columns in the original file as dates (datetime) are being returned as floating point numbers. I need to change the number back to datetime.
Libraries used:
library(readxl)
library(odbc)
library(lubridate)
Number
44047.8149884259
44055.2403009259
44048.504537037
Expected result
8/4/2020 7:33:35 PM
8/12/2020 5:46:02 AM
8/5/2020 12:06:32 PM
I've tried to use as_date with different formats and as.POSIXct but don't seem to have an answer.
Thanks in advance for your kindly inputs.
We could use
format(as.POSIXct(v1 * 60 *60 * 24, origin = '1899-12-30', tz = 'UTC'),
'%m/%d/%Y %I:%M:%S %p')
#[1] "08/04/2020 07:33:34 pm" "08/12/2020 05:46:01 am" "08/05/2020 12:06:31 pm"
data
v1 <- c(44047.8149884259, 44055.2403009259, 44048.504537037)
This question already has answers here:
Transform year/week to date object
(2 answers)
Closed 4 years ago.
The variable I want to convert is an integer in the form of YYYYWW.
So, for example 200901 represents week 1 of 2009, 201223 --> 23 of 2012 and so on.
I want to convert this variable into date format based on weeks in a year.
So in my example 01-2009 and 23-2012 or a similar format. I already tried several lubridate and ISOweek functions but never come up with a good result.
I really appreciate your help.
Not sure if it works for weeks but try the solution here:
https://stackoverflow.com/a/29928301/5335354
Something like:
df <- transform(df, x = as.Date(as.character(x), "%Y%U"))
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 9 years ago.
I have a data in the following format 200101 and I want it to be in the following format 2001-01 or 2001/01
Thanks
I don't deal with dates so there may well be better approaches. Your problem is you have no day. I know the zoo package can handle this but not in the format you want. I also give a regex approach but this is not a date class, just character.
As date:
library(zoo)
as.yearmon("200101", "%Y%m")
## > as.yearmon("200101", "%Y%m")
## [1] "Jan 2001"
As character:
gsub("([0-9]{4})","\\1-", "200101")
## > gsub("([0-9]{4})","\\1-", "200101")
## [1] "2001-01"
## gsub("([0-9]{4})","\\1/", "200101")
This question already has answers here:
Add correct century to dates with year provided as "Year without century", %y
(3 answers)
Closed 5 years ago.
I have a date column in a data frame in chr format as follows:
chr [1:1944] "20-Sep-90" "24-Feb-05" "16-Aug-65" "19-Nov-56" "28-Nov-59" "19-Apr-86"
I want to convert to date using something like:
strptime(x=data$dob, '%d-%b-%y')
But I get several future dates in the result like
[1] "1990-09-20" "2005-02-24" "2065-08-16" "2056-11-19" "2059-11-28" "1986-04-19" "2041-04-01" "1971-01-23"
[9] "1995-11-25" "1995-11-25" "2009-02-11" "2002-09-19" "1977-10-06" "1998-03-22" "2050-03-12" "2030-03-26"
Is there a way to ensure I return dates that commenced in the correct century?
Thanks
It doesn't look (from the documentation for %y in ?strptime) like there's any obvious option for changing the default century inferred from 2-digit years.
Since the objects returned by strptime() have class POSIXlt, though, it's a pretty simple matter to subtract 100 years from any dates after today (or after any other cutoff date you'd like to use).
# Use strptime() to create object of class POSIXlt
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
DD <- strptime(dd, '%d-%b-%y')
# Subtract 100 years from any date after today
DD$year <- ifelse(DD > Sys.time(), DD$year-100, DD$year)
DD
[1] "1990-09-20" "2005-02-24" "1965-08-16" "1956-11-19" "1959-11-28" "1986-04-19"
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
library(lubridate)
DD=dmy(dd)
https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html http://vita.had.co.nz/papers/lubridate.pdf
strptime(data$dob, "%Y/%m/%d")