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