Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use mutate ifelse gether date in r

I want to use dplyr::mutate find gether date.

example
I want to find data of EDS>2020-10-01 ,but my code is fail.

test1 <-  data %>%
   mutate(g = ifelse(  (EDS > "2020-10-01" & `price`>0 ) | 
              (is.na(EDS) & `price`>0 ) , 1 , 0))

What is the right way of coding this?
Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


Warning message:
Problem while computing g = ifelse(...).
ℹ Incompatible methods ("Ops.POSIXt", "Ops.Date") for ">"

>Solution :

The column is POSIXlt class which is a list. Converting to Date class should work

library(dplyr)
data %>%
   mutate(EDS = as.Date(EDS), g = ifelse(  (EDS > "2020-10-01" & `price`>0 ) | 
              (is.na(EDS) & `price`>0 ) , 1 , 0))

Otherwise, the comparison with character class or Date class should have worked

> "2019-01-01" > "2020-01-01"
[1] FALSE
> "2019-01-01" > "1999-01-01"
[1] TRUE

data

data <- data.frame(EDS = c("1990-01-01", "2020-10-01", 
     "2021-10-01"), price = c(1.5, 0, 0.5))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading