Add date of character class with another date - r

Can we add the date of Character class to another date (lag of specific date). I want to reduce by 05:30:00
df
Date
12:48:36
12:48:37
13:48:36
Required dateframe
df
Date
07:48:36
07:48:37
08:48:36
df <- structure(list(Date = structure(1:3, .Label = c("12:48:36", "12:48:37",
"13:48:36"), class = "factor")), class = "data.frame", row.names = c(NA,
-3L))

You could use as.ITime from data.table
library(data.table)
setDT(df)
df[, Date := as.ITime(Date) - as.ITime('05:00:00')]
df
# Date
# 1: 07:48:36
# 2: 07:48:37
# 3: 08:48:36
Edit: If you have stored Date as a factor (as in this example) you need to convert to character first
df[, Date := as.character(Date)]

Related

max(DF1$EFFDT) <= DF2$EFFDT [duplicate]

This question already has an answer here:
Searching for nearest date in data frame
(1 answer)
Closed 1 year ago.
I have two dataframes, DF1 containing monthly data snapshot of data whereas DF2 with a particular date and i want to be able to retrieve data only for closest maxdate (<=) from DF1 wrt DF2 data.
DF1
Account
Date
A1000001
1-JAN-2021
A1000002
1-FEB-2021
A1000003
1-MAR-2021
A1000004
1-APR-2021
DF2
Date
15-MAR-2021
Output Expected:
Account
Date
A1000003
1-MAR-2021
Change the dates to actual date class and using sapply you may find the closest date in df1 for each date in df2.
df1$Date <- as.Date(df1$Date, '%d-%b-%Y')
df2$Date <- as.Date(df2$Date, '%d-%b-%Y')
result <- df1[sapply(df2$Date, function(x) which.min(abs(df1$Date - x))), ]
result
# Account Date
#3 A1000003 2021-03-01
data
It is easier to help if you provide data in a reproducible format
df1 <- structure(list(Account = c("A1000001", "A1000002", "A1000003",
"A1000004"), Date = c("1-JAN-2021", "1-FEB-2021", "1-MAR-2021",
"1-APR-2021")), row.names = c(NA, -4L), class = "data.frame")
df2 <- structure(list(Date = "15-MAR-2021"), row.names = c(NA, -1L),
class = "data.frame")

Convert the date into an individual date and time in R

I would like to convert a date that I have in R into an individual date and time. At the moment the format of the date is POSIXct
An example is given here:
"2019-03-29 20:42:07"
I want the date to be in one column and the time of that date in a corresponding column. I have found something similar here, but it doesn't answer my question.
Many thanks
If the column shows POSIXct class. Create two new columns by coercing to Date (as.Date) and the time part with format
df1 <- transform(df1, date = as.Date(datetime), time = format(datetime, "%T"))
df1
# datetime date time
#1 2019-03-29 20:42:07 2019-03-30 20:42:07
data
df1 <- structure(list(datetime = structure(1553910127, class = c("POSIXct",
"POSIXt"), tzone = "")), class = "data.frame", row.names = c(NA,
-1L))

Convert Factor to only Time in R

I have a got a dataframe having factor columns as shown below
df
ColA
14:59:33.0000000
15:59:33.0000000
16:59:33.0000000
17:59:33.0000000
ColA is a factor. Can we concert them to only time
Expected Output
df
ColA
14:59:33
15:59:33
16:59:33
17:59:33
Using strptime and format.
format(strptime(v, "%T"), "%T")
# [1] "14:59:33" "15:59:33" "16:59:33" "17:59:33"
Data
v <- structure(1:4, .Label = c("14:59:33.0020000", "15:59:33.0000000",
"16:59:33.0000000", "17:59:33.0000000"), class = "factor")
We can use as.ITime
library(data.table)
as.ITime(as.character(v))
#[1] "14:59:33" "15:59:33" "16:59:33" "17:59:33"
data
v <- structure(1:4, .Label = c("14:59:33.0020000", "15:59:33.0000000",
"16:59:33.0000000", "17:59:33.0000000"), class = "factor")

Subset data frame by rows containing the system date

I would like to subset a data frame by selecting only the rows with the current system date.
For example, I have this data frame:
df = data.frame("var" = c("A", "A", "B", "B"),
"date" = c("2020-03-01", "2020-03-17",
"2020-03-01", "2020-03-17"))
df$date = as.POSIXct(df$date, format = "%Y-%m-%d")
If today is 2020-03-17, I would like to subset the rows that contain only the current date.
I have tried the following:
df_today = df[which(df$date == Sys.Date()),]
Which gives the error:
Warning message: In which(df$date == Sys.Date()) :
Incompatible methods ("Ops.POSIXt", "Ops.Date") for "=="
I have also tried:
df[which(df$date == as.POSIXct(Sys.Date())),]
Which returns an empty data frame. What I found works is if I coerce the date column as a character and then subset the rows in this way:
df$date = as.character(df$date)
df[which(df$date == as.character(Sys.Date)),]
This can work, but I would like to know where I am going wrong with my my previous attempts and if there is a better way than converting back and forth between character and POSIXct?
Thank you in advance for any input!
Class "Date" is not the same as class "POSIXct", you need to convert first to the former using local Sys.timezone().
df[as.Date(df$date, tz=Sys.timezone()) == Sys.Date(),]
# var date
# 2 A 2020-03-17
# 4 B 2020-03-17
Data used
df <- structure(list(var = structure(c(1L, 1L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), date = structure(c(1583017200, 1584399600,
1583017200, 1584399600), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA,
-4L), class = "data.frame")
library(dplyr)
df$date = as.Date(df$date, format = "%Y-%m-%d")
df %>% filter(date==Sys.Date())

Convert monthly data from one type to another

I have a column with monthly date that has such type (number of month, _, year):
Date
9_2018
1_2013
12_2014
etc.
I want to convert this date format to a date of the following form (year, month):
New_Date
201809
201301
201412
How can I do this?
We can use zoo::as.yearmon to convert the date and then use format to get data in the required format.
format(zoo::as.yearmon(df$Date, "%m_%Y"), "%Y%m")
#[1] "201809" "201301" "201412"
Or can be done in base R as well by pasting an arbitrary date to year-month value we have.
format(as.Date(paste0("1_", df$Date), "%d_%m_%Y"), "%Y%m")
data
df <- structure(list(Date = structure(c(3L, 1L, 2L), .Label = c("1_2013",
"12_2014", "9_2018"), class = "factor")),
class = "data.frame",row.names = c(NA, -3L))
Padding zeros for month and then splitting at the "_":
library(stringr)
mon <- sapply(strsplit(Date, "_"), FUN="[", 1)
mon <- str_pad(mon, width=2, pad="0")
year <- sapply(strsplit(Date, "_"), FUN="[", 2)
paste0(year,mon)
[1] "201809" "201301" "201412"

Resources