Extracting rows from start date and end dates that contain specific value in R

I have a data frame which has a column of ID numbers and dates each time ID logged into website. i need to calculate the total number of times the 'ID' logged into the site between specified interval.

I want to create a function that will input login_check(startdate, enddate, ID) in argument create a new dataframe column of ID number and each row reflects her each login attempt within time interval

Then i can use the length() function to calculate total number of logins within time frame

Can someone please help me with creating the dataframe for specific ID within time frame?

Data:

structure(list(Order_ID = c(69L, 3025L, 3549L, 27L, 4561L, 2429L ), Date = structure(c(17899, 17921, 17925, 17923, 17917, 17919 ), class = "Date")), row.names = c(NA, 6L), class = "data.frame")

1 answer

  • answered 2020-10-16 04:46 Paul

    dplyr::filter can be used to filter a dataset based on your conditions.

    logins <- structure(list(Order_ID = c(69L, 3025L, 3549L, 27L, 4561L, 2429L ), Date = structure(c(17899, 17921, 17925, 17923, 17917, 17919 ), class = "Date")), row.names = c(NA, 6L), class = "data.frame") 
    
    login_check <- function(startdate, enddate, ID) {
      dplyr::filter(logins, Order_ID == ID, Date >= startdate, Date <= enddate)
    }
    
    login_check('2019-01-01', '2020-01-01', 3025)
    #> Order_ID       Date
    #> 1     3025 2019-01-25
    

    Or, if you prefer base R

    login_check <- function(startdate, enddate, ID) {
      logins[logins$Order_ID == ID & logins$Date >= startdate & logins$Date <= enddate,]
    }
    

    Or using subset

    login_check <- function(startdate, enddate, ID) {
      subset(logins, Order_ID == ID & Date >= startdate & Date <= enddate)
    }