s157
s157

Reputation: 453

comparing dates in R not working well (equal)

I want to compare 2 column dataframe with dates and include one column to indicate whether dates "A" are <= dates "B" or >

df <- data.frame( list (A=c("15-10-2000", "15-10-2000", "15-10-2000","20-10-2000"),                     
                        B=c("15-10-2000", "16-10-2000", "14-10-2000","19-10-2000")))

What I would like to include is new column C = ( 1 , 1, 0, 0).

I have tried:

df$C = ifelse (df$A <= df$B, 1, 0)

It works except for the "equal" comparation.

I get: C = ( 0 , 1, 0, 0)

sorry but before doing the comparation I changed the format to Date and still does not works

df$A= as.Date(df$A, format = "%d-%m-%Y")
df$B = as.Date(df$B, format = "%d-%m-%Y")

Upvotes: 0

Views: 2277

Answers (2)

MrNetherlands
MrNetherlands

Reputation: 940

You should convert the factors to dates (As Jon Spring pointed out). Then it should work

library(dplyr)
df %>% 
  mutate_all(lubridate::dmy) %>%
  mutate(C = ifelse(A<=B,1,0))

           A          B C
1 2000-10-15 2000-10-15 1
2 2000-10-15 2000-10-16 1
3 2000-10-15 2000-10-14 0
4 2000-10-20 2000-10-19 0

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 389047

The date columns are factors. You need to first convert them to Date class and then compare

library(dplyr)

df %>%
  mutate_at(vars(A:B), as.Date, format = "%d-%m-%Y") %>%
  mutate(C = as.integer(A <= B))

#           A          B C
#1 2000-10-15 2000-10-15 1
#2 2000-10-15 2000-10-16 1
#3 2000-10-15 2000-10-14 0
#4 2000-10-20 2000-10-19 0

Or in base R that would be

df[1:2] <- lapply(df[1:2], as.Date, format = "%d-%m-%Y")
df$C <- as.integer(df$A <= df$B)

Upvotes: 2

Related Questions