3

I have an excel file which has two columns start_date and end_date. I want to compare the end_date with the system date but everytime I use this code I get an error:

Incompatible methods ("ops.Date", "Ops.POSIXt") for "<"

I am reading excel file using

x<-read_excel("book.xlxs", Header="TRUE")

And now I am using

Myframe<-ifelse(x$end_date>Sys.Date())

What to do now?

Abdou
  • 12,931
  • 4
  • 39
  • 42
Suraj
  • 41
  • 7
  • Please, revise your code. There are many syntactical errors. – Uwe Aug 03 '17 at 16:50
  • 1
    Try `Myframe<-ifelse(x$end_date>as.POSIXct(Sys.Date()), 'TRUE_VAL', 'FALSE_VAL')`. `Sys.Date` does not return a `POSIXct` object; so you will need to coerce it into one. – Abdou Aug 03 '17 at 16:52
  • Or maybe you want `Myframe <- x$end_date > Sys.Date()`. But these will only work if `class(x$end_date)` is `Date` - you'll probably need to do that conversion. – Gregor Thomas Aug 03 '17 at 16:55
  • This i think worked but Can you tell me please what this does as I am new to r – Suraj Aug 03 '17 at 16:59
  • Also, the first one you wrote that gives an output logical (0) . what does that mean? – Suraj Aug 03 '17 at 17:07

1 Answers1

1

The error comes from the fact that you are trying to compare two objects of different classes. The call to Sys.Date returns an object of class Date. You can check that by calling class(Sys.Date). This returns Date. Moreover, it looks like you are using the readxl package, which usually imports datetime columns as POSIXct objects. As a result, you are trying to compare POSIXct objects with Date objects. This will almost always cause errors. As recommend in the comments, you should either convert Sys.Date to a POSIXct object with the function as.POSIXct or you can convert the values in the column end_date to Date objects. I recommend you do the first because you're only converting one value with the following line of code:

Myframe<-ifelse(x$end_date > as.POSIXct(Sys.Date()), 'TRUE_VAL', 'FALSE_VAL')

If no value in the end_date is greater than as.POSIXct(Sys.Date()), the comparison x$end_date > as.POSIXct(Sys.Date()) will return an empty vector. That's why you are seeing logical(0). This means that it is an R vector of logical values. But since it is empty, R will return logical(0).

If you changed the comparison to x$end_date <= as.POSIXct(Sys.Date()), you may see a vector of logical values (TRUE or FALSE).

Furthermore, if you're looking to have a new column that tracks which rows in the column end_date passed the comparison test, you can do the following:

x$end_date_passed <- ifelse(x$end_date > as.POSIXct(Sys.Date()), 'yes', 'no')

The new column here is called end_date_passed.

I hope this helps.

Abdou
  • 12,931
  • 4
  • 39
  • 42
  • But when I see the output of Myframe it gives me logical(0) . what does that mean? – Suraj Aug 03 '17 at 17:24
  • But some dates in end_date are greater than current date! – Suraj Aug 03 '17 at 17:40
  • Also when I am using <= it still shows logical(0) . what can be wrong ? – Suraj Aug 03 '17 at 17:41
  • Another question I want to ask you is that how to update a column say for example end_date_passed having values yes or no with new values according to the comparison between end_date and sys_date? – Suraj Aug 03 '17 at 17:55
  • @Suraj please the edits. If you have any additional questions, please create a new post. This question is already answered. Please considered marking it answered with the check mark next to it. – Abdou Aug 03 '17 at 18:02