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

Changing specific column values based on conditions in R

I have following dataframe named ‘Saty’ in R

One_Day Arrived_Date Departure_Date
Yes 2022-01-10 NA
No 2021-05-12 2021-06-01
Yes 2021-12-01 2021-12-01
Yes 2022-03-01 NA

I want to modify the dataframe, if the One_Day column value is ‘Yes’, my ‘Arrived_Date’ and ‘Departure_Date’ should be same, like below dataframe.

One_Day Arrived_Date Departure_Date
Yes 2022-01-10 2022-01-10
No 2021-05-12 2021-06-01
Yes 2021-12-01 2021-12-01
Yes 2022-03-01 2022-03-01

How to achieve this by coding in R ?

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

>Solution :

What you need is an ifelse statement.

library(dplyr)

Saty %>% mutate(Departure_Date = ifelse(One_Day == "Yes", Arrived_Date, Departure_Date))

  One_Day Arrived_Date Departure_Date
1     Yes   2022-01-10     2022-01-10
2      No   2021-05-12     2021-06-01
3     Yes   2021-12-01     2021-12-01
4     Yes   2022-03-01     2022-03-01
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