Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Date/Time to Filter in R

Tags:

r

dplyr

I'm using R and Dplyr and a dataset that has one column with date/time information, one column that has phone numbers and one column that has two options, Eggs and Cheese.

      Date Phone.Number Eggs.or.Cheese
1 14/09/15   1111111111           EGGS
2 14/09/15   2222222222           EGGS
3 14/09/15   3333333333           EGGS
4 15/09/15   4444444444           EGGS
5 15/09/15   5555555555           EGGS
6 16/09/15   1111111111         CHEESE
7 16/09/15   6666666666           EGGS
8 16/09/15   7777777777           EGGS  

(Dput Information here):

structure(list(Date = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 
        3L), .Label = c("14/09/15", "15/09/15", "16/09/15"), class = "factor"), 
            Phone.Number = c(1111111111, 2222222222, 3333333333, 4444444444, 
            5555555555, 1111111111, 6666666666, 7777777777), Eggs.or.Cheese = structure(c(2L, 
            2L, 2L, 2L, 2L, 1L, 2L, 2L), .Label = c("CHEESE", "EGGS"), class = "factor")), .Names = c("Date", 
        "Phone.Number", "Eggs.or.Cheese"), class = "data.frame", row.names = c(NA, 
        -8L))

I am trying to create a subset that includes all phone numbers that have indicated eggs in the past and then have called cheese. This subset would include every observation for those phone numbers and would look something like below.

      Date Phone.Number Eggs.or.Cheese
1 14/09/15   1111111111           EGGS
2 16/09/15   1111111111         CHEESE

I've been playing with filters, but I'm unsure how to use date and time information within commands

Also, I'm still new to R, coding and stackfoverflow, so any feedback on how I ask questions would be appreciated.

like image 202
Lyriss Avatar asked Dec 06 '25 06:12

Lyriss


1 Answers

Here's an attempt using data.table.

First, we will convert Date to a proper class so we could sort by it, then we check unique combinations per phone and see if they are matching "EGGS, CHEESE", then print the whole group

library(data.table)
setDT(DT)[, Date := as.IDate(Date, "%d/%m/%y")]
DT[order(Date), if(toString(unique(Eggs.or.Cheese)) == "EGGS, CHEESE") .SD, by = Phone.Number]
#    Phone.Number       Date Eggs.or.Cheese
# 1:   1111111111 2015-09-14           EGGS
# 2:   1111111111 2015-09-16         CHEESE

A dplyr equivalent would be

library(dplyr)
DT %>%
  mutate(Date = as.Date(Date, "%d/%m/%y")) %>%
  arrange(Date) %>% ## This is optional if your data is already sorted
  group_by(Phone.Number) %>%
  filter(toString(unique(Eggs.or.Cheese)) == "EGGS, CHEESE")

# Source: local data frame [2 x 3]
# Groups: Phone.Number [1]
# 
#         Date Phone.Number Eggs.or.Cheese
#       (date)        (dbl)         (fctr)
# 1 2015-09-14   1111111111           EGGS
# 2 2015-09-16   1111111111         CHEESE
like image 124
David Arenburg Avatar answered Dec 08 '25 18:12

David Arenburg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!