#!/usr/local/bin/R # Create_Read_CSV.R # Let us try to create and read CSV # Load tidyverse library(tidyverse) # Get current working directory currentdir <- getwd() currentdir # Create new working directory newdir <- paste0(currentdir,"/HW_Answers", sep = "") dir.create(newdir, recursive = TRUE) # Set working directory setwd(newdir) # You can check current directory getwd() # Our goal is to create a CSV file with the following and calculate the sums: # Placebo Vaccine # Infected 162 8 # Not Infected 18163 18190 # Create a data frame Placebo <- c(162, 18163) Vaccine <- c(8, 18190) vacctrial <- data.frame(Placebo, Vaccine) vacctrial rownames(vacctrial) rownames(vacctrial) <- c("Infected", "Not_Infected") rownames(vacctrial) vacctrial infsum <- sum(vacctrial[1,]) notinfsum <-sum(vacctrial[2,]) psum <- sum(vacctrial[,1]) vsum <- sum(vacctrial[,2]) allsum <- psum + vsum vacctrial$Sum <- c(infsum, notinfsum) vacctrial vacctrial <- rbind(vacctrial, c(psum, vsum, allsum)) rownames(vacctrial) <- c("Infected", "Not_Infected", "Sum2") vacctrial # or you can put it in one command vacctrial2 <- data.frame(Placebo = c(162, 18163), Vaccine =c(8, 18190)) rownames(vacctrial2) <- c("Infected", "Not_Infected") vacctrial2 vacctrial2 <- as.table(vacctrial2) infsum <- sum(vacctrial2[1,]) notinfsum <-sum(vacctrial2[2,]) psum <- sum(vacctrial2[,1]) vsum <- sum(vacctrial2[,2]) allsum <- psum + vsum vacctrial2$Sum <- c(infsum, notinfsum) vacctrial2 vacctrial2 <- rbind(vacctrial2, c(psum, vsum, allsum)) rownames(vacctrial2) <- c("Infected", "Not_Infected", "Sum2") vacctrial2 # or you can create a matrix vacctrial3 <- matrix(c(162, 18163, 8, 18190), nrow=2, ncol=2) vacctrial3 rownames(vacctrial3) <- c("Infected", "Not_Infected") colnames(vacctrial3) <- c("Placebo", "Vaccine") vacctrial3 vacctrial3 <- as.table(vacctrial3) vacctrial3 <- addmargins(vacctrial3) vacctrial3 dir() # There is nothing since there is no file in the newly created directory # Let us create the CSV file (all three files below should be the same) write.csv(vacctrial, file = paste0(newdir,"/Vaccine_Trial.csv")) write.csv(vacctrial2, file = paste0(newdir,"/Vaccine_Trial2.csv")) write.csv(vacctrial3, file = paste0(newdir,"/Vaccine_Trial3.csv")) # Now let us read the CSV file trialres <- read.csv(file = paste0(newdir,"/Vaccine_Trial.csv")) trialres trialres2 <- read.csv(file = paste0(newdir,"/Vaccine_Trial2.csv")) trialres2 trialres3 <- read.csv(file = paste0(newdir,"/Vaccine_Trial3.csv")) trialres3 # See the difference when you read it, I will use trialres here # To avoid this, you can also use row.names = FALSE when you write the CSV file # e.g., write.csv(vacctrial, file = paste0(newdir,"/Vaccine_Trial.csv"), row.names = FALSE) trialres[,1] trialres[,2] trialres[,3] infp <- as.numeric(trialres[1,2]) notinfp <- as.numeric(trialres[2,2]) totalp <- infp + notinfp totalp infv <- as.numeric(trialres[1,3]) notinfv <- as.numeric(trialres[2,3]) totalv <- infv + notinfv totalv riskv <- (infv/totalv) * 100 riskv riskp <- (infp/totalp) * 100 riskp # Relative Risk (%) rel_risk <- (riskv/riskp) * 100 rel_risk # Attributable Risk (%) att_risk <- (riskp-riskv) att_risk # Number Needed to Treat nnt <- 1/(att_risk/100) round(nnt)