Subset data frame by rows containing the system date - r

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

Related

How to apply a specific function to typer character columns only in a dataset?

My test dataset
df=structure(list(Atencion = c(871739L, 866903L, 847986L, 872950L,
860503L, 868579L), NomAtenTipoBase = c("Hospitalización", "Hospitalización",
"Hospitalización", "Urgencias", "Hospitalización", "Hospitalización"
), FecIngreso = structure(c(1656598680, 1656161220, 1654693680,
1656675480, 1655690640, 1656423480), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), Plan_Del_Contrato = c("ATENCIÓN PGP ONCOLÓGICO SUBSIDIADO - CONTRIBUTIV",
"SANTANDER-C", "PBS-C", "ACCIDENTES DE TRANSITO", "ATENCIÓN INTEGRAL ONCOLOGIA REG- SUBSIDIADO",
"ARL")), row.names = c(NA, 6L), class = "data.frame")
I require to recursively apply this function only to the columns with character type (NomAtenTipoBase and Plan_Del_Contrato), instead of applying the code on each column:
df$NomAtenTipoBase = stri_enc_toutf8(df$NomAtenTipoBase)
df$Plan_Del_Contrato = stri_enc_toutf8(df$Plan_Del_Contrato)
We could use across with where
library(dplyr)
library(stringi)
df <- df %>%
mutate(across(where(is.character), stri_enc_toutf8))
Or with base R , find the character column and update only those by looping over the subset of columns
i1 <- sapply(df, is.character)
df[i1] <- lapply(df[i1], stri_enc_toutf8)

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

Add date of character class with another date

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

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"

Returning first row of group

I have a dataframe consisting of an ID, that is the same for each element in a group, two datetimes and the time interval between these two. One of the datetime objects is my relevant time marker. Now I like to get a subset of the dataframe that consists of the earliest entry for each group. The entries (especially the time interval) need to stay untouched.
My first approach was to sort the frame according to 1. ID and 2. relevant datetime. However, I wasn't able to return the first entry for each new group.
I then have been looking at the aggregate() as well as ddply() function but I could not find an option in both that just returns the first entry without applying an aggregation function to the time interval value.
Is there an (easy) way to accomplish this?
ADDITION:
Maybe I was unclear by adding my aggregate() and ddply() notes. I do not necessarily need to aggregate. Given the fact that the dataframe is sorted in a way that the first row of each new group is the row I am looking for, it would suffice to just return a subset with each row that has a different ID than the one before (which is the start-row of each new group).
Example data:
structure(list(ID = c(1454L, 1322L, 1454L, 1454L, 1855L, 1669L,
1727L, 1727L, 1488L), Line = structure(c(2L, 1L, 3L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("A", "B", "C"), class = "factor"),
Start = structure(c(1357038060, 1357221074, 1357369644, 1357834170,
1357913412, 1358151763, 1358691675, 1358789411, 1359538400
), class = c("POSIXct", "POSIXt"), tzone = ""), End = structure(c(1357110430,
1357365312, 1357564413, 1358230679, 1357978810, 1358674600,
1358853933, 1359531923, 1359568151), class = c("POSIXct",
"POSIXt"), tzone = ""), Interval = c(1206.16666666667, 2403.96666666667,
3246.15, 6608.48333333333, 1089.96666666667, 8713.95, 2704.3,
12375.2, 495.85)), .Names = c("ID", "Line", "Start", "End",
"Interval"), row.names = c(NA, -9L), class = "data.frame")
By reproducing the example data frame and testing it I found a way of getting the needed result:
Order data by relevant columns (ID, Start)
ordered_data <- data[order(data$ID, data$Start),]
Find the first row for each new ID
final <- ordered_data[!duplicated(ordered_data$ID),]
As you don't provide any data, here is an example using base R with a sample data frame :
df <- data.frame(group=c("a", "b"), value=1:8)
## Order the data frame with the variable of interest
df <- df[order(df$value),]
## Aggregate
aggregate(df, list(df$group), FUN=head, 1)
EDIT : As Ananda suggests in his comment, the following call to aggregate is better :
aggregate(.~group, df, FUN=head, 1)
If you prefer to use plyr, you can replace aggregate with ddply :
ddply(df, "group", head, 1)
Using ffirst from collapse
library(collapse)
ffirst(df, g = df$group)
data
df <- data.frame(group=c("a", "b"), value=1:8)
This could also be achieved by dplyr using group_by and slice-family of functions,
data %>%
group_by(ID) %>%
slice_head(n = 1)

Resources