感谢大家对processing of hospital admission data using R这个问题提出的建议,我对这个问题有其他问题,实际上应该是这个问题之前的任务。使用R处理医院门诊数据(第二部分)
现在我有一个这样的数据集:
Patient_ID Date Ward
P001 1 A
P001 2 A
P001 3 A
P001 4 A
P001 4 B
P001 5 B
P001 6 B
P001 7 B
P001 7 C
P001 8 B
P001 9 B
P001 10 B
我需要将其转换成:
Patient_ID Date Ward
P001 1 A
P001 2 A
P001 3 A
P001 4 A;B
P001 5 B
P001 6 B
P001 7 B;C
P001 8 B
P001 9 B
P001 10 B
目前我一直在使用ddply
转换它,代码如下附:
data <- ddply(data,
c("Patient_ID", "Date"),
function(df)
{data.frame(Ward=paste(unique(df[,"Ward"]),collapse=";"))
},
.progress="text"
)
这可以解决我的问题,但它非常慢(超过20分钟在P4 3.2 mac hine)当数据集有8818 unique(Patients_ID)
和1861 unique(Date)
。我该如何改进?谢谢!