0

I am trying to clean this dataset and the original data is formatted terribly. This is the format that the original data came in:

PATIENT_CODE     PATIENT_NAME     TEST           TEST_RESULT
1                JOHN SMITH       URIC ACID      5
1                JOHN SMITH       GLUCOSE        6

This is the format I would like to use:

PATIENT_CODE     PATIENT_NAME    URIC ACID     GLUCOSE
1                JOHN SMITH      5             6

There are 10+ tests ("TEST" variable) for each patient, and not all patients received all of the tests. How do I "unmelt" this data to get the desired dataset?

jay.sf
  • 46,523
  • 6
  • 46
  • 87
Carmen
  • 11

1 Answers1

0

This is a dcast()

library(reshape2)

> dcast(df1, ... ~ TEST, value.var="TEST_RESULT")
  PATIENT_CODE PATIENT_NAME GLUCOSE URIC ACID
1            1  JOHN SMITH        6         5

Data

df1 <- structure(list(PATIENT_CODE = c(1L, 1L), PATIENT_NAME = structure(c(1L, 
1L), .Label = "JOHN SMITH ", class = "factor"), TEST = structure(2:1, .Label = c("GLUCOSE", 
"URIC ACID"), class = "factor"), TEST_RESULT = 5:6), row.names = c(NA, 
-2L), class = "data.frame")
jay.sf
  • 46,523
  • 6
  • 46
  • 87