血液数据格式转换

图1 原始导出来的数据


图2 转换为分析格式

本代码可以实现把图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")

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容