This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 3 years ago.
I am trying to convert months in the following format:
histdata <- c("198001", "198002", "198003")
I tried:
histdata <- transform(histdata, date = as.Date(as.character(date), "%Y%m"))
but then all data turned to NA.
I would like to find a solution that will return a vector of dates instead.
You could try
convert_date <- function(x)
{
x <- as.character(x)
as.POSIXct(paste0(substr(x, 1, 4), "-", substr(x, 5, 6), "-01"))
}
So that you have:
convert_date(c(198002, 198003, 202001))
# [1] "1980-02-01 GMT" "1980-03-01 GMT" "2020-01-01 GMT"
library(readr)
ym <- c("198001", "198002", "198003")
parse_date(ym, format = "%Y%m")
or
library(readr)
ym <- c(198001, 198002, 198003)
parse_date(as.character(ym), format = "%Y%m")
You can use base function as.Date with first day of the month added to your representation of the month:
as.Date(paste0(as.character(201901L), "01"), format = "%Y%m%d")
Related
This question already has an answer here:
format year-month to POSIXct [duplicate]
(1 answer)
Closed 2 years ago.
I am trying to convert a set of values to POSIXct class, but it keeps returning NA's. I only have the month and year, and the column is in "YYYY-MM" format.
Here is an snippet of the data I have:
yr_mo
"2012-08"
"2012-08"
"2012-08"
Here is the code I have that is returning NA's:
yr_mo <- as.POSIXct(strptime(x=yr_mo, format = "%Y-%m", tz= "America/Los_Angeles"))
I have tried different formats like "%Y-%m-%d" and no luck. I have also tried as.Date and that also did not work.
I am trying to get this data ready for ArcGIS.
You may paste a phantom day.
as.POSIXct(paste0(d$yr_mo, "-01"), tz="GMT")
# [1] "2012-08-01 GMT" "2012-08-01 GMT" "2012-08-01 GMT"
Data:
d <- read.table(text='yr_mo
"2012-08"
"2012-08"
"2012-08"', header=TRUE)
Using lubridate
library(lubridate)
ymd(df$yr_mo, truncated = 1)
#[1] "2012-08-01" "2012-08-01" "2012-08-01"
Or to convert to POSIXct
ymd_hms(df$yr_mo, truncated = 4)
#[1] "2012-08-01 UTC" "2012-08-01 UTC" "2012-08-01 UTC"
data
df <- structure(list(yr_mo = c("2012-08", "2012-08", "2012-08")),
class = "data.frame", row.names = c(NA, -3L))
Using R
How do we convert "yyyymm" to "yyyy-mm-01" across all the rows?
Eg: "201603" to "2016-03-01" (ie "yyyy-mm-dd" format)
PS: Here, (dd = 01) is the default date for all 12 months. ie("2016-01-01" , "2016-02-01" , etc...)
A simple paste solution:
x <- "201603"
paste0(substr(x, 1,4), "-", substr(x, 5,6), "-01")
[1] "2016-03-01"
If you want to transform as date:
as.Date(paste0(201603, 01), format = "%Y%m%d")
This will create the 2016-03-01 format as the date and not as a character.
If you want to use in all rows on column Date
data <- data %>%
mutate(Date = as.Date(paste(Date, 01) format = "%Y%m%d")
additional solution
library(lubridate)
library(stringr)
x <- c("201603")
ymd(str_c(x,"01"))
[1] "2016-03-01"
This question already has answers here:
Split date-time column into Date and time variables
(7 answers)
Closed 4 years ago.
Date Assigned
<dttm>
2017-09-02 14:25:00
I am wondering how to separate the date from the time, into two separate columns, for easier plotting.
I have tried the following code, but it has returned NA;
Hours <- format(as.POSIXct(strptime(INV$`Date Assigned`, "%Y/%m/%d %H:%M", tz ="")),
format = "H%:%M")
Dates <- format(as.POSIXct(strptime(INV$`Date Assigned`,"%Y/%m/%d",tz="")),
format = "%d/%d/%Y")
Any suggestions?
Try:
Hours <- format(as.POSIXct(INV$`Date Assigned`, "%Y-%m-%d %H:%M:%S", tz = ""), format = "%H:%M")
Dates <- format(as.Date(INV$`Date Assigned`,"%Y-%m-%d"), format = "%d/%m/%Y")
Output:
Hours
[1] "14:25"
Dates
[1] "02/09/2017"
Of course if you want them as columns in your dataframe, you need to assign them as such:
INV$Hours <- format(as.POSIXct(INV$`Date Assigned`, "%Y-%m-%d %H:%M:%S", tz = ""), format = "%H:%M")
INV$Dates <- format(as.Date(INV$`Date Assigned`,"%Y-%m-%d"), format = "%d/%m/%Y")
Output:
Date Assigned Hours Dates
1 2017-09-02 14:25:00 14:25 02/09/2017
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")
Currently my dataframe has dates displayed in the 'Date' column as 01/01/2007 etc I would like to convert these into a week/year value i.e. 01/2007. Any ideas?
I have been trying things like this and getting no where...
enviro$Week <- strptime(enviro$Date, format= "%W/%Y")
You have to first convert to date, then you can convert back to the week of the year using format, for example:
### Converts character to date
test.date <- as.Date("10/10/2014", format="%m/%d/%Y")
### Extracts only Week of the year and year
format(test.date, format="Week number %W of %Y")
[1] "Week number 40 of 2014"
### Or if you prefer
format(date, format="%W/%Y")
[1] "40/2014"
So, in your case, you would do something like this:
enviro$Week <- format(as.Date(enviro$Date, format="%m/%d/%Y"), format= "%W/%Y")
But remember that the part as.Date(enviro$Date, format="%m/%d/%Y") is only necessary if your data is not in Date format, and you also should put the right format parameter to convert your character to Date, if that is the case.
What is the class of enviro$Date? If it is of class Date there is probably a better way of doing this, otherwise you can try
v <- strsplit(as.character(enviro$Date), split = "/")
weeks <- sapply(v, "[", 2)
years <- sapply(v, "[", 3)
enviro$Week <- paste(weeks, years, sep = "/")