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 easily handle multiple similar cods

I want to search "831" from several columns (ECG1 to ECG20). I wrote following codes, but them is so lengthy. I wonder if there is a simple and smart way to handle these codes.

Thank you very much.

af$af_exist <- 0

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

af <- af[ECG1 %in% c("831"), af_exist := 1]

af <- af[ECG2 %in% c("831"), af_exist := 1]

af <- af[ECG3 %in% c("831"), af_exist := 1]

af <- af[ECG4 %in% c("831"), af_exist := 1]

af <- af[ECG5 %in% c("831"), af_exist := 1]

af <- af[ECG6 %in% c("831"), af_exist := 1]

af <- af[ECG7 %in% c("831"), af_exist := 1]

af <- af[ECG8 %in% c("831"), af_exist := 1]

……

af <- af[ECG20 %in% c("831"), af_exist := 1]

>Solution :

af %>%
  mutate(af_exists = +if_any(ECG1:ECG20,~.== "831"))
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