I am working with a wide dataset and want to convert it to a long dataset with Pivot_Longer. The original dataset has 1000 plus columns with different ICD10 codes, I want to conver them into a long format and call the column "diag". The value then will be 1 if the original field has value, else 0. I was successful till converting the table to long, but it still has the actual ICD10 codes rather than 1 or 0 value. How can I create a new field dummy = 0 or 1, depending on the column "sum":
if sum is blank, then 0, else 1??
longdata<- pivot_longer(iris4, cols = starts_with("ICD"), names_to = "Diag", values_to = "sum")
longdata$dummy <- 1
str(longdata)
replace(longdata$dummy, longdata$sum == "", 0 )
X2nd.MD.ID Gender Age Zip Date.of.Service Diag sum
1 196602 M 0.0583 60301 12/10/2019 ICD_1 "P034"
2 196602 M 0.0583 60301 12/10/2019 ICD_2 "Q5310"
3 196602 M 0.0583 60301 12/10/2019 ICD_3 "Z23"
4 196602 M 0.0583 60301 12/10/2019 ICD_4 "Z3801"
5 196602 M 0.0583 60301 12/10/2019 ICD_5 ""
6 196602 M 0.0583 60301 12/10/2019 ICD_6 ""
7 196602 M 0.0583 60301 12/10/2019 ICD_7 ""
8 196602 M 0.0583 60301 12/10/2019 ICD_8 ""
9 196602 M 0.0583 60301 12/10/2019 ICD_9 ""
10 196602 M 0.0583 60301 12/10/2019 ICD_10 ""