简介:汇总医学数据处理中所用的数据生成和修改代码
R包:tidyverse
变量类型设置和转换
variables <-c("mage_c2","fage_c2","M_edu","F_edu" ,"M_job" ,"F_job","parity_c2" ,"pcaWI_C3","treat_chris","ac203_c2")
adam <- adam %>% mutate(across(all_of(variables),as.factor))
计算新变量-纵向
#求eGFR
#设置自定义函数
for(i in 1:53){
diff_col_name <- paste("eGFR", i, sep = "") # 创建新列的名称
年龄 <- paste0("入院年龄",i)
血肌酐 <- paste0("首次随访-血肌酐Scr-",i)
result <- pmap_dbl(data1[,c("性别",年龄,血肌酐)], ~eGFR(..1,..2,..3))
result <- as_tibble(result)
result <- set_names(result,diff_col_name)
dataresult<-bind_cols(dataresult,result)
}
c <- pmap_dfc(data1[,c("性别","年龄-1","首次随访-血肌酐Scr-1")], ~eGFR(..1,..2,..3))
c1 <- c %>% pivot_longer()
for(i in 1:53){
diff_col_name <- paste("eGFR", i, sep = "") # 创建新列的名称
年龄 <- paste0("入院年龄",i)
血肌酐 <- paste0("首次随访-血肌酐Scr-",i)
result <- pmap_dbl(data1[,c("性别",年龄,血肌酐)], ~eGFR(..1,..2,..3))
result <- as_tibble(result)
result <- set_names(result,diff_col_name)
dataresult<-bind_cols(dataresult,result)
}
计算新变量-横向
分组求值
pivot_pri <- pivot_pri %>%
group_by(编号) %>%
mutate(discharge = ifelse(row_number() == n(), 1, 0))
pivot_pri <- left_join(pivot_pri,pri[,c(1:13)],by=join_by(编号))