R语言基本操作

以SOFA 评分表为例:


image.png
  1. 模拟数据
pao2 <- round(rnorm(100,mean = 300,sd = 30))
summary(pao2)

fio2 <- round(rnorm(100,mean = 0.5,sd = 0.1),2)
gcs <- round(rnorm(100,mean = 80,sd = 20)/10)
map <- round(rnorm(100,mean = 65,sd = 15))
dop <- round(rnorm(100,mean = 10,sd = 3),1)

dob <- rbinom(100,1,0.4)
epi <- round(rnorm(100,mean = 10,sd = 3)/100,2)
nor <- round(rnorm(100,mean = 10,sd = 3)/100,2)

bilirubin <- round(rnorm(100,mean = 80,sd = 20),1)
platelet <- round(rnorm(100,mean = 180,sd =50))
cr <- round(rnorm(100,mean = 150,sd = 34))
uo <- rnorm(100, mean = 1000, sd = 500)

uo <- round(ifelse(uo >0, uo,-uo))  # 使其不出现负数

data <- data.frame(pao2,fio2,gcs,map,dop,dob,epi,nor,bilirubin,platelet,cr,uo)
head(data,4)

#    pao2 fio2 gcs map  dop dob  epi  nor bilirubin platelet  cr   uo
# 1  306 0.55  10  60  9.1   0 0.13 0.07      57.8      152 113 2158
# 2  306 0.62  10  75 12.5   1 0.14 0.14      53.5      237 126  436
# 3  274 0.51   9  91  9.8   1 0.11 0.07      68.0      135 104  868
# 4  312 0.38   8  63  7.8   1 0.07 0.11      78.2      190 133 1230
  1. 将呼吸、凝血、肝功指标转化为相应评分
data$respiratory <- ifelse(data$pao2/data$fio2 >= 400,0,
                           ifelse(data$pao2/data$fio2 >= 300,1,
                                  ifelse(data$pao2/data$fio2 >= 200,2,
                                         ifelse(data$pao2/data$fio2 >= 100,3,4))))

data$coagulation <- ifelse(data$platelet >= 150,0,
                           ifelse(data$platelet >= 100,1,
                                  ifelse(data$platelet >= 50,2,
                                         ifelse(data$platelet >= 20,3,4))))

data$liver <- ifelse(data$bilirubin >= 204,4,
                     ifelse(data$bilirubin >= 102,3,
                            ifelse(data$bilirubin >= 33,2,
                                   ifelse(data$bilirubin >= 20,1,0))))

head(data[,c("pao2","fio2","respiratory","coagulation","liver")])

#  pao2 fio2 respiratory coagulation liver
# 1  306 0.55           0           0     2
# 2  306 0.62           0           0     2
# 3  274 0.51           0           1     2
# 4  312 0.38           0           0     2
# 5  348 0.52           0           1     2
# 6  271 0.56           0           0     3
  1. 转化神经系统、肾功、心功指标为相应评分
# 中枢
data$neuro <- cut(data$gcs,breaks = c(3,5,9,12,14,15),labels = c(4,3,2,1,0),include.lowest = TRUE)
data$neuro <- as.numeric(as.character(data$neuro)) #将上一步的结果为factor
data$neuro
# 肾功
cr.score <- ifelse(data$cr < 110,0,
                   ifelse(data$cr <= 170,1,
                          ifelse(data$cr <= 229,2,
                                 ifelse(data$cr <= 440,3,4))))
uo.score <- ifelse(data$uo >= 500,0,ifelse(data$uo >200,3,4))
data$renal <- max(cr.score,uo.score)
# 循环
map.score <- ifelse(data$map >= 70,0,1)
dop.score <- ifelse(data$dop > 15,4,ifelse(data$dop >5.1,3,2))
dob.score <- ifelse(data$dob == 1,2,0)
epi.score <- ifelse(data$epi <= 0.1,3,4)
nor.score <- ifelse(data$nor > 0.1,4,3)

data$cardio <- max(map.score,dop.score,dob.score,epi.score,nor.score)

# sofa 评分
data$sofa.score <- rowSums(data[,c("cardio","renal","coagulation","liver","neuro","respiratory")])
head(data)
image.png
  1. 连续变量重新编码
data$oxyindex <- data$pao2/data$fio2
data$berlin <- cut(data$oxyindex, breaks = c(min(data$oxyindex),
                                             100,200,300,max(data$oxyindex)),
                   labels = c("severe","moderate","mild","none"),include.lowest = TRUE)
table(data$berlin)

# severe moderate     mild     none 
#    0        0        1       99 
  1. 修改变量名
# method 1
names(data)[20] <- "oxygen.index"
names(data)[11] <- "creatinine"
names(data)

# method 2
library(reshape)
data <- rename(data,c(gcs = "GCS",berlin = "Berlin"))
names(data)

参考资料
文中代码来自章仲恒教授的丁香园课程:R语言基本操作

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

推荐阅读更多精彩内容