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

Create column with a specific elements conditioned to the other column

I have the following script:

library(lubridate)
library(tidyverse)

date_vec <- seq(dmy("01-11-2020"), dmy("15-07-2022"), by = "day")

df <- tibble(date = date_vec, description = NA)

limpmerg <- dmy("04-11-2020", "16-11-2020", "25-11-2020", "03-12-2020", 
                "20-12-2020", "28-12-2020", "08-01-2021", "21-01-2021", 
                "28-01-2021", "24-02-2021", "06-03-2021", "11-03-2021", 
                "22-03-2021", "30-03-2021", "04-04-2021", "19-04-2021", 
                "28-04-2021", "25-05-2021", "08-06-2021", "15-06-2021", 
                "21-06-2021", "24-06-2021", "30-06-2021", "09-07-2021", 
                "15-07-2021")

falhequip <- dmy("20-11-2020", "21-11-2020", "23-11-2020", "24-11-2020",
                 "25-11-2020", "04-01-2021", "05-01-2021", "06-01-2021", 
                 "07-01-2021", "24-01-2021", "25-01-2021", "26-01-2021")

I would add in column description, of df a text "clean" to dates in vector limpmerg, a text "failing" to dates in vector falhequip. To the other dates, I would add text "collect".

How can I do this?

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

Thank’s

>Solution :

We may use case_when

library(dplyr)
df1 <- df %>%
  mutate(description = case_when(date %in% limpmerg ~ "clean", 
                                date %in% falhequip ~ "failing",
                                 TRUE ~ "collect"))
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