我正在尝试清理此数据集,并且原始数据的格式非常糟糕。这是原始数据输入的格式:
PATIENT_CODE PATIENT_NAME TEST TEST_RESULT
1 JOHN SMITH URIC ACID 5
1 JOHN SMITH GLUCOSE 6
这是我要使用的格式:
PATIENT_CODE PATIENT_NAME URIC ACID GLUCOSE
1 JOHN SMITH 5 6
每位患者有10项以上的测试(“ TEST”变量),并且并非所有患者都接受了所有测试。如何“解散”这些数据以获得所需的数据集?
答案 0 :(得分:0)
这是dcast()
library(reshape2)
> dcast(df1, ... ~ TEST, value.var="TEST_RESULT")
PATIENT_CODE PATIENT_NAME GLUCOSE URIC ACID
1 1 JOHN SMITH 6 5
数据
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")