Date.Time column split - r

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))

Related

making 2 digit month and day columns in R

I'm very new to R, so this might seem straightforward. But I have a data frame with an original date column that has values that look like this: 4-02-91, 5-29-93 (i.e. m-d-y). I am trying to separate this column into 3, where months, days, and years are separate. Then I need to combine them again to this format 19910402, 19930529 - I need it this way in order to compare it to another dataset with similar dates.
Here is what I've been trying to do:
# Make DATE an actual date column
dataframe$DATE <- as.Date(used$DATE, format="%m-%d-%Y")
# This changes the original date column into something that looks like this: 1991-04-02, 1993-05-29
# Separate DATE into multiple columns
dataframe$year <- year(dataframe$DATE)
dataframe$month <- month(dataframe$DATE)
dataframe$day <- day(dataframe$DATE)
# Combine dates again to get string
dataframe$raster_date<-paste(dataframe$year, dataframe$month, dataframe$day, sep = "")
The last step looks great except where the months or days are single digits. It's coming out as 199142 and 1993529 instead of 19910402 and 19930529. How do I insert zeros when the month and day values are 1 digit?
Here, we can use sprintf instead of paste as the year, month, day from lubridate extracts those as numeric values and numeric class would drop the 0 padded as prefix. We add those prefix with 0s in sprintf
sprintf("%04d%02d%02d", dataframe$year, dataframe$month, dataframe$day)

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")

How do I delete rows in a data frame based on the value (date) in one of the columns?

I have a data frame that consists of daily data. It has 500,000+ rows and 18 columns. The 2nd column contains the date.
For example, it goes from 7/1/2017 to the current date, chronologically.
I pull the data every Monday and input it into R, but I only want data up until the most recent Friday.
I've set a variable equal to the most recent Friday's date (in the exact date format of the data):
library(lubridate)
LastFriday <- gsub("X", "", gsub("X0", "", format(
Sys.Date() - wday(Sys.date()+1), "X%m/X%d/%Y)))
which returns 9/15/2017
How do I delete all the rows in the data frame after the last row that contains last Friday's date?
The following should work, though I have not tested it
keep_index <- as.POSIXct(as.Date(df[,2]), "X%m/X%d/%Y") <= as.POSIXct(LastFriday, format = "X%m/X%d/%Y")
mydf <- df[keep_index, ]

Converting variables in form of "2015M01" to date format in 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.

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