I have a dataframe with two columns(both are dates) and a million rows. I have to compare both the dates and return value in the third column. i.e if date in column A is greater
In base
:
DF$C <- as.numeric(DF$A > DF$B)
In dplyr
:
DF %>%
mutate(C = as.numeric(A > B))
library(data.table)
dt <- as.data.table(dt)
dt$A <- as.Date(dt$A)
dt$B <- as.Date(dt$B)
Here are two ways you can try:
dt[, C := ifelse(A > B, 1, 0)]
or
dt[, C := 0][A > B, C := 1]
In second way, you can change to dt[, C := 1][A <= B, C := 0]
by checking which has less obs.
Maybe you need to provide a little reproducible example.