R: How to filter/subset a sequence of dates
you could use subset
Generating your sample data:
temp<-
read.table(text="date sessions
2014-12-01 1932
2014-12-02 1828
2014-12-03 2349
2014-12-04 8192
2014-12-05 3188
2014-12-06 3277", header=T)
Making sure it's in date format:
temp$date <- as.Date(temp$date, format= "%Y-%m-%d")
temp
# date sessions
# 1 2014-12-01 1932
# 2 2014-12-02 1828
# 3 2014-12-03 2349
# 4 2014-12-04 8192
# 5 2014-12-05 3188
# 6 2014-12-06 3277
Using subset
:
subset(temp, date> "2014-12-03" & date < "2014-12-05")
which gives:
# date sessions
# 4 2014-12-04 8192
you could also use []
:
temp[(temp$date> "2014-12-03" & temp$date < "2014-12-05"),]
R: Subsetting a data frame using a list of dates as the filter
You have to convert the date string
into a Date
variable using as.Date
(try ?as.Date
at the console). Bonus: you can drop which:
> testdf[testdf$mydate %in% as.Date(c('2012-01-05', '2012-01-09')),]
mydate col1 col2 col3
5 2012-01-05 5 15 25
9 2012-01-09 9 19 29
Filter Dates from Dataframe RStudio
you do not need a function. Try:
train[!train$date %in% dates, ]
as for getting a date range between
train[train$date >= '2020-04-15' & date <= '2020-06-05', ]
subset a dataframe in R within a specific time range
Here's a dplyr
solution:
library(dplyr)
dataBase %>%
mutate(date = as.Date(date, format = "%d/%m/%Y")) %>%
filter(date >= "2020-07-30" & date <= "2020-08-30")
a date
V12 -0.23017749 2020-08-28
V13 1.55870831 2020-08-01
V21 0.07050839 2020-08-27
V32 -1.26506123 2020-08-01
V41 -0.44566197 2020-08-28
V43 0.35981383 2020-08-01
V52 0.11068272 2020-08-01
Data:
set.seed(123)
dataBase <- data.frame(a = rnorm(15), date = unlist(read.table(text = '"30/06/2020" "27/08/2020" "30/06/2020" "28/08/2020" "30/06/2020"
"28/08/2020" "30/06/2020" "01/08/2020" "30/06/2020" "01/08/2020"
"01/08/2020" "30/06/2020" "30/06/2020" "01/08/2020" "30/06/2019"')))
How do I subset a specific date range in R?
How about something like this?
First of all, you should provide some data that is easier to reproduce e.g use dput. Secondly, your date formats are mixed up.
library(tidyverse)
library(lubridate)
df <- tibble(DateTime = dmy_hm("01/1/2016 11:10", "03/10/2016 12:16", "05/05/2017 13:17", "15/04/2018 14:11", "20/05/2018 15:21"),
Course = c("CS", "Eng", "Poetry", "Reading", "Maths"),
Prof_in_time = c("Morgan", "Andrew", "Jen", "Eugene", "Matt"))
df #note typos in the date format in your data
Then you could do this:
start <- dmy_hm("01/1/2016 00:00") #note you had different formats
end <- dmy_hm("31/10/2016 23:59")
df2 <- df %>%
filter(DateTime >= start & DateTime <= end)
df2
This filters your dates. This might not match yours as I played around with the date formats.
Or you could do this instead:
df3 <- df %>%
filter(DateTime >= dmy_hm("01/1/2016 00:00") & DateTime <= dmy_hm("31/10/2016 23:59"))
df3
I have a column of dates, how to filter dates before a specific day? R
library(lubridate)
df <- setDT(your_dataset)
df_sep <- df[mydates >= as.Date('2020-09-01') & mydates <= as.Date('2020-09-30')]
Filter data based on date/time range and matching id
Here are 2 approaches :
- Fuzzy joins based on range :
fuzzyjoin::fuzzy_inner_join(dataloggers, data,
by = c('id', 'datetime' = 'starttime', 'datetime'),
match_fun = list(`==`, `>=`, `<=`))
Join by
id
and keep data in range -a.
dplyr
:
library(dplyr)
dataloggers %>%
inner_join(data, by = 'id') %>%
filter(datetime.x >= starttime & datetime.x <= datetime.y)
b. Base R :
subset(merge(dataloggers, data, by = 'id'),
datetime.x >= starttime & datetime.x <= datetime.y)
Related Topics
Select Rows of a Matrix That Meet a Condition
Why Is Message() a Better Choice Than Print() in R for Writing a Package
Colour Points in a Plot Differently Depending on a Vector of Values
Add an Index (Numeric Id) Column to Large Data Frame
Creating a Unique Sequence of Dates
Why Does "One" < 2 Equal False in R
Convert String to Date, Format: "Dd.Mm.Yyyy"
Compile R Script into Standalone .Exe File
Ggplot2 Shade Area Under Density Curve by Group
Inserting a Table Under the Legend in a Ggplot2 Histogram
Create a Matrix of Scatterplots (Pairs() Equivalent) in Ggplot2
How to Save Data File into .Rdata
What Leads the First Element of a Printed List to Be Enclosed with Backticks in R V3.5.1