user17163779
user17163779

Reputation:

Function to check if there is a date in my database

Could you help me tweak this function below: basically it's a function that checks if the given date I choose is in my Test database. If not, it will show "This date is not in your Test database, otherwise it will show "This date is in your Test database".

#database

Test <- structure(
  list(dat= c("2021-01-01","2021-01-02","2021-01-03"),
       X= c(5,4,0)),
  class = "data.frame", row.names = c(NA, -3L))
        

firstf <- function() {
if (The date is not in my database #I don't know how to write this here){
  print ("This date is not in your Test database)}
else {
print ("This date is in your Test database)
}
}

firstf("2021-01-01")

So, for example, I do firstf("2021-01-01"), is to look like "This date is in your test database". If I enter firstf("2021-01-15") it will show "This date is not in your test database

Upvotes: 0

Views: 38

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389175

You can check for the date using %in%.

firstf <- function(database, date) {
  if (date %in% database$dat)
      print ("This date is in your Test database")
  else 
  print("This date is not in your Test database")
}

firstf(Test, "2021-01-01")
#[1] "This date is in your Test database"

firstf(Test, "2021-01-15")
#[1] "This date is not in your Test database"

Upvotes: 1

Related Questions