garyshyu
garyshyu

Reputation: 1

Find Date in Data Frame in R

I'm trying to use a list of dates to find the same date in a data frame and using ifelse loop to add a new column. Here is the code:

library(lubridate)
df <- tibble(DateCol = seq(ymd("2021-01-01"),ymd("2021-05-31"),"days"))

df2 <- c("2021-02-04","2021-02-07","2021-02-17","2021-02-25")

for (i in 1:length(df$DateCol)) {
  if (df$DateCol[i] == df2) {
    df$ValueCol[i] <- "1"
    } else {
      df$ValueCol[i] <- "0"
      return(i)
    }
  }

The final df I get only has the first date of df2 is 1 in df$ValueCol. Not sure how to make this loop work, seems like there are some mistakes in the df$DateCol[i] == df2 part.

Upvotes: 0

Views: 370

Answers (2)

nyk
nyk

Reputation: 680

Or you can try using dplyr

library(dplyr)
library(magrittr)
df <- tibble(DateCol = seq(ymd("2021-01-01"),ymd("2021-05-31"),"days"))

df2 <- ymd("2021-02-04","2021-02-07","2021-02-17","2021-02-25")
df3 <- df %>% mutate(ValueCol = if_else(DateCol %in% df2, 1, 0))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389135

You can do this without a loop.

df$ValueCol <- as.integer(df$DateCol %in% as.Date(df2))
df

as.integer is faster way than using ifelse with 1 and 0 as output.

df$ValueCol <- ifelse(df$DateCol %in% as.Date(df2), 1, 0)

Your loop can be corrected by using %in% instead of ==, as we use %in% to compare more than 1 value.

library(tibble)
library(lubridate)

df <- tibble(DateCol = seq(ymd("2021-01-01"),ymd("2021-05-31"),"days"))

df2 <- as.Date(c("2021-02-04","2021-02-07","2021-02-17","2021-02-25"))
df$ValueCol <- NA
for (i in 1:length(df$DateCol)) {
  if (df$DateCol[i] %in% df2) {
    df$ValueCol[i] <- 1
  } else {
    df$ValueCol[i] <- 0
  }
}

Upvotes: 1

Related Questions