本代码可以实现把图1的常规检验数据导出格式转换为图2的数据分析格式,按序号和检验日期排序,二者不对应哈
#读取数据
dataRaw = read.csv("blood.csv",header =T)
#使用unique函数去除向量中的重复数据
vars = unique(dataRaw$LAB_ITEM_NAME)
ptsid = unique(dataRaw$INP_NO)
time = unique(dataRaw$REPORT_DATE_TIME)
#定义一个空白数据框
res = matrix(NA, nrow = length(vars), ncol = 0)
rownames(res) = vars
for(i in 1:length(ptsid) ){
id = ptsid[i]
sub.dat = dataRaw[dataRaw$INP_NO==id, ]
t2 = unique(sub.dat$REPORT_DATE_TIME)
for(j in 1:length(t2) ){
tt = t2[j]
dat = sub.dat$RESULT[sub.dat$REPORT_DATE_TIME == tt]
name = sub.dat$LAB_ITEM_NAME[sub.dat$REPORT_DATE_TIME == tt]
match.dat = dat[match(vars, name) ]
old.name = colnames(res)
res = cbind(res, match.dat)
colnames(res) = c(old.name, paste(id, tt, sep = "_"))
}
}
res = t(res) #转置
write.csv(res, "Sorted_data_abnormal-Na.csv")