Converting variables in form of "2015M01" to date format in R? - r

I have a date frame df that simply looks like this:
month values
2012M01 99904
2012M02 99616
2012M03 99530
2012M04 99500
2012M05 99380
2012M06 99103
2013M01 98533
2013M02 97600
2013M03 96431
2013M04 95369
2013M05 94527
2013M06 93783
with month that was written in form of "M01", "M02"... and so on.
Now I want to convert this column to date format, is there a way to do it in R with lubridate?
I also want to select columns that contain one certain month from each year, like only March columns from all these years, what is the best way to do it?

The short answer is that dates require a year, month and day, so you cannot convert directly to a date format. You have 2 options.
Option 1: convert to a year-month format using zoo::as.yearmon.
library(zoo)
df$yearmon <- as.yearmon(df$month, "%YM%m")
# you can get e.g. month from that
months(df$yearmon[1])
# [1] "January"
Option 2: convert to a date by assuming that the day is always the first day of the month.
df$date <- as.Date(paste(df$month, "01", sep = "-"), "%YM%m-%d")
For selection (and I think you mean select rows, not columns), you already have everything you need. For example, to select only March 2013:
library(dplyr)
df %>% filter(month == "2013M03")

Something like this will get it:
raw <- "2012M01"
dt <- strptime(raw,format = "%YM%m")
dt will be in a Posix format. The strptime function will assign a '1' as the default day of month to make it a complete date.

Related

Date.Time column split

I am trying to split the Date.Time column in my data table to separate date and time columns. currently the column is as character.
this is what I already tried but it just gave me a column with 2019 dates. I don't want the year to be 2019 so doesn't work. even if it does, not sure how to get the time to a separate column
office$date <- as.Date(office$Date.Time, format = '%m/%d')
office$date <- as.Date(office$Date.Time, format = '%m/%d')
Date require the year field. You can remove the year field, but the result will be a character, not the date format.
office$date <- as.Date(office$Date.Time, format = 'Y%/%m/%d')
office$date <- as.character(gsub("^.{5}","",office$date))

Can I match a character string containing m-d with a date vector in R?

All, Ive seen that date conversion questions get downvoted a lot, but I couldn't find any information online or in the help files...
I have a df with a date formatted as ymd_hm() and then some data in other columns. Then I have another df with 366 row, one for each day, and a column containing some values relevant for that day (some climatological stuff, that is essentially the same every year, so the year doesn't matter). The dfs might look something like this:
df1 <- tibble(Date=seq(ymd_hm('2010-05-01 00:00'),ymd_hm('2010-05-03 00:00'), by = 'hour'), Data=c(1:length(Date)))
df2 <- tibble(MonthDay=c("04-30", "05-01", "05-02","05-03","05-04"), OtherData=c(20,30,40,50, 60))
Now, is it possible to do some lookup sort of thing and match Date and MonthDay and then write whatever OtherData is into df1? I'm struggling since I can't convert MonthDay to a date.
So, all the 2010-05-01 dates should have 30 next to them, all 2010-05-02 dates should have 40 in the next column, and so on and so forth...
Thanks y'all!
We extract the 'MondayDay' with format, use that as common joining column in left_join
library(dplyr)
df1 %>%
mutate(MonthDay = format(Date, "%m-%d")) %>%
left_join(df2) %>%
select(-MonthDay)

How to add a separate column to data frame with lubridate? Would like to have numerical month and word month in the data frame?

The lubridate allows us to break down y-m-d format to month, year week, etc... I have done this with my data set. I have the months in numerical months, but want a separate column with month abbreviations. I can convert them, but I want to have both numerical and word month in the data frame. Is there another way to go about doing this besides manually adding a column vector?
lubridate::month generates the numerical month. Adding the argument label = TRUE generates the month abbreviation. You can use dplyr::mutate to add the new column.
For example:
library(dplyr)
library(lubridate)
data.frame(Date = as_date("2001-10-11")) %>%
mutate(Month = month(Date),
MonthAbb = month(Date, label = TRUE))
Date Month MonthAbb
1 2001-10-11 10 Oct

Converting string to date in R returns NAs

I have a column of my dataframe as
date
17-Feb
17-Mar
16-Dec
16-Nov
16-Sep
17-Feb
I am trying to convert it into a date column from string. I am using the following pieces of code:
as.Date(df$Date, format="%y-%b")
and
as.POSIXct(df$Date, format="%y-%b")
Both of them give NAs
I am getting the format from this link
The starting number is year. Sorry for the confusion.
I assume from your approach that the 17 and 16 refer to the year 2017 and 2016 respectively. You need to also specify the day of month. If you don't care about it, then set it to the 1st.
A slight modification to your code will work, by appending '-01' to the date then updating your format argument to reflect this:
df = data.frame(Date = c("17-Feb", "17-Mar", "16-Dec"))
as.Date(paste0(df$Date, "-01"), format="%y-%b-%d")

Sorting the date column in calendar order in R

Is there a way to sort the date column in R in calendar order. like begining from "Jan-16", "Feb-16", "Mar-16" or beginning with recent month "May-16", "Apr-16" and "Mar-16".
Regards,
Mohan
One solution is to add the year, and then convert the vector to the Date class:
# dates
dates <- c("Jan-16", "Feb-16", "Mar-16")
# convert to date class
dates <- as.Date(paste0("2016-", dates), format="%Y-%b-%d")
# get most recent date
max(dates)
# sort
sort(dates, decreasing=T)

Resources