I have a dataset of patient info, 25 DX codes, and 20 Procedure codes. Each code is in its own column so my table is 45 columns wide just between diagnosis and procedure codes. I need to get the dx codes into two columns DX1-25, and the the code, and Procedure code into and Proc1-proc20 and the codes.
Parsed down original data for ease of use
MRN <- c(1,2,3,4)
DX1 <- c('12','14','16','m78.2')
DX2 <- c('m46.2', 'z98.0', 'z86.711', 'm10.6')
DX3 <- c('m10.7', 'Z86.711', 'M45.1', 'K21.9')
PROC1 <- c(06030, 06020, 06047, 22585)
PROC2 <- c(63020, 63030, 63047, 63030)
PROC3 <- c(22551, 22558, 22528, 22558)
spine_pt_3 <- as.data.frame(cbind(MRN, DX1, DX2, DX3, PROC1,PROC2, PROC3))
Code attempted to get data in desired format
spine3 <- melt(setDT(spine_pt_3),
id = 1,
measure1 = list(2:4),
measure2 = list (5:7),
Variable1= "DX",
variable2 = "Proc"
)
My goal is to get my data to look like this. I'm not sure if this is possible or when I'm going wrong.