Commit 28a2dfed authored by Cecilia Akerlund's avatar Cecilia Akerlund

Upload New File

parent d3aafb34
rm(list=ls())
library(ggplot2)
library(gridExtra)
library(dplyr)
library(tidyr)
nas <- c("", NA)
Baseline <- read.csv("C:/Users/Cecilia KI/Dropbox/Cecilia/ICP burden/Baseline.new_200625.csv", stringsAsFactors = FALSE, na.strings = nas)
colnames(Baseline)
Baseline <- Baseline %>% mutate(icp.mean = ifelse(is.na(icp.mean), icp.evd.mean, icp.mean),
icp.median = ifelse(is.na(icp.median), icp.evd.median, icp.median),
cpp.mean = ifelse(is.na(cpp.mean), cpp.evd.mean, cpp.mean),
cpp.median = ifelse(is.na(cpp.median), cpp.evd.median, cpp.median))
Cont.var <- c("Age", "GCSMotorBaselineDerived", "GCSScoreBaselineDerived", "TotalISS", "TotalISS.Extracran", "ExtracranISSHighest", "BestOfHeadBrainCervicalISS",
"LengthOfStay", "ICULOS", "monitor.time", "TIL.mean", "TIL.max", "GOSE6mo", "icp.mean", "icp.median", "DVTemp.mean", "cpp.mean",
"cpp.median", "RotterdamCTScore", "Sodium")
Cat.var <- c("SiteCode", "Sex", "InjCause", "InjMech", "ASA", "GOSE6mo", "PupilsBaseline", "EDHypoxia", "EDHypotension", "HosComplEventHypoxia",
"HosComplEventHypotension", "ICPDevice")
Binary.var <- c("Contusion", "CisternalCompression", "SkullFracture", "MidlineShift", "MassLesion", "tSAH", "EDH", "aSDH", "cSDH", "IVH", "DecompressiveCran")
# Summaries: ---------------------------
median(Baseline$GOSE6mo)
quantile(Baseline$GOSE6mo, 0.25)
quantile(Baseline$GOSE6mo, 0.75)
# Continuous vars:---------------------
Baseline.cont <- Baseline %>% select(Cont.var)
Mean.Summaries <- Baseline.cont %>% summarise_all(mean, na.rm = TRUE)
Mean.Summaries <- data.frame(round(t(Mean.Summaries), 2))
Mean.Summaries$Feature <- rownames(Mean.Summaries)
SD.Summaries <- Baseline.cont %>% summarise_all(sd, na.rm=TRUE)
SD.Summaries <- data.frame(round(t(SD.Summaries), 2))
SD.Summaries$Feature <- rownames(SD.Summaries)
Cont.Summaries <- left_join(Mean.Summaries, SD.Summaries, by="Feature")
Median.Summaries <- Baseline.cont %>% summarise_all(median, na.rm=TRUE)
Median.Summaries <- data.frame(round(t(Median.Summaries), 2))
Median.Summaries$Feature <- rownames(Median.Summaries)
for (i in 1:nrow(Median.Summaries))
{
temp <- Median.Summaries$Feature[i]
Median.Summaries$lb[i] <- quantile(Baseline[,which(colnames(Baseline) == temp)],na.rm=TRUE, 0.25)
Median.Summaries$ub[i] <- quantile(Baseline[,which(colnames(Baseline) == temp)],na.rm=TRUE, 0.75)
}
# Median.Summaries <- Median.Summaries %>% mutate(IQR = paste0(round(lb,1), "-", round(ub,1))) %>% select(-lb, -ub)
Cont.Summaries <- left_join(Cont.Summaries, Median.Summaries, by="Feature") %>% select(Feature, everything())
colnames(Cont.Summaries) <- c("Feature", "Mean", "SD", "Median", "lb", "ub")
Cont.Summaries <- Cont.Summaries %>% mutate("Mean +- SD" = paste0(Mean, "+-", SD), "Median (IQR)" = paste0(Median, " (",round(lb,1), "-", round(ub,1),")")) %>%
select(Feature, "Mean +- SD", "Median (IQR)")
rm(Mean.Summaries, Median.Summaries, SD.Summaries)
# Binary: ---------------------------
Baseline.bin <- Baseline %>% select(all_of(Binary.var))
Baseline.bin.count <- Baseline.bin %>% summarise_all(sum, na.rm = TRUE)
Baseline.bin.count <- data.frame(t(Baseline.bin.count))
Baseline.bin.count$Feature <- rownames(Baseline.bin.count)
Baseline.bin.frac <- Baseline.bin %>% summarise_all(mean, na.rm =TRUE)
Baseline.bin.frac <- data.frame(t(round(Baseline.bin.frac*100,1)))
Baseline.bin.frac$Feature <- rownames(Baseline.bin.frac)
Bin.Summaries <- left_join(Baseline.bin.count, Baseline.bin.frac, by="Feature") %>% mutate(Category="") %>% select(Feature, Category, everything())
colnames(Bin.Summaries) <- c("Feature", "Category", "n", "percent")
Bin.Summaries <- Bin.Summaries %>% mutate('n (%)' = paste0(n, " (", percent, ")")) %>% select(-n, -percent)
rm(Baseline.bin.count, Baseline.bin.frac)
# Categorical: --------------------
Cat.Summaries <- data.frame(n=integer(), Freq=numeric(), Features=character())
for(i in 2:length(Cat.var))
{
Temp <- as.data.frame(table(Baseline %>% select(Cat.var[i]))) %>% rename(n=Freq)
Temp.frac <- as.data.frame(prop.table(table(Baseline %>% select(Cat.var[i]))))
Temp <- left_join(Temp, Temp.frac, by="Var1")
Temp$Feature <- Cat.var[i]
Cat.Summaries <- rbind(Cat.Summaries, Temp)
}
Cat.Summaries <- Cat.Summaries %>% select(Feature, Category = Var1, n, Freq)
Cat.Summaries <- Cat.Summaries %>% mutate(Category = as.character(Cat.Summaries$Category), 'n (%)' = paste0(n, " (", round(Freq*100,1), ")")) %>%
select(-n, -Freq)
Cat.Summaries <- Cat.Summaries %>% filter(Feature != "InjMech")
Catbin.Summaries <- rbind(Bin.Summaries, Cat.Summaries)
length(unique(Baseline$SiteCode))
# Missing data:
Missings <- data.frame(DataAvailable = colSums(!is.na(Baseline)))
Missings$Feature <- rownames(Missings)
Catbin.Summaries <- left_join(Catbin.Summaries, Missings, by="Feature") %>% select(Feature, DataAvailable, Category, 'n (%)')
Cont.Summaries <- left_join(Cont.Summaries, Missings, by="Feature") %>% select(Feature, DataAvailable, everything())
rm(Baseline.bin, Baseline.cont, Temp, Temp.frac, Bin.Summaries, Cat.Summaries, Missings)
View(Catbin.Summaries)
View(Cont.Summaries)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment