From 9f374a72c873d01a8592d12308f97b9bf00a21b4 Mon Sep 17 00:00:00 2001 From: Megan Schmidt Date: Wed, 17 Nov 2021 00:17:36 -0500 Subject: [PATCH 1/4] First attempt --- Tutorial9.R | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Tutorial9.R diff --git a/Tutorial9.R b/Tutorial9.R new file mode 100644 index 0000000..002e294 --- /dev/null +++ b/Tutorial9.R @@ -0,0 +1,18 @@ +setwd("C:/Users/megan/Desktop/r-novice-inflammation/r-novice-inflammation-data/data") +dir <- list.files("../data", full.names = TRUE) + +fileCOV <- c() +coeffientOfVariation <- function(dir){ + + +} +wage <- read.csv(file="wages.csv", header = TRUE, stringsAsFactors = FALSE) +y <- sapply(wage,class) +z <- colnames(wage) +print(paste("Which column of wages.csv would you like to calculate the coefficient of variation for? The columns are" , z , "/" ,y)) +print("Please only select a column of integers or numerics.") +x <- as.name(readline()) +mean <- mean(wage[[x]]) +std <- sd(wage[[x]]) +cov <- std/mean +fileCOV <- c(fileCOV, cov) From dca8ea9a002df719e1752e2b34c0d55ecd4be708 Mon Sep 17 00:00:00 2001 From: Megan Schmidt Date: Wed, 17 Nov 2021 23:47:36 -0500 Subject: [PATCH 2/4] progress --- Tutorial9.R | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/Tutorial9.R b/Tutorial9.R index 002e294..6a5e520 100644 --- a/Tutorial9.R +++ b/Tutorial9.R @@ -1,18 +1,39 @@ setwd("C:/Users/megan/Desktop/r-novice-inflammation/r-novice-inflammation-data/data") dir <- list.files("../data", full.names = TRUE) - +dir fileCOV <- c() + coeffientOfVariation <- function(dir){ - + csv <- dir[grep(".csv",dir)] + table <- dir[grep(".txt",dir)] + usable <- c(csv,table) + for (i in 1:length(usable)){ + if (usable[grep(".csv", usable)] == TRUE){ + fil <- read.csv(file = usable[i], header = TRUE, stringsAsFactors = FALSE) + } + else { + fil <- read.table(file = usable[i], sep = ,header = TRUE, stringsAsFactors = FALSE) + } + y <- sapply(fil,class) + z <- colnames(fil) + print(paste("Which column of the file would you like to calculate the coefficient of variation for? The columns are" , z , "/" ,y)) + print("Please only select a column of integers or numerics.") + x <- as.name(readline()) + mean <- mean(fil[[x]]) + std <- sd(fil[[x]]) + cov <- std/mean + fileCOV <- c(fileCOV, cov) + i = i + 1 + } } -wage <- read.csv(file="wages.csv", header = TRUE, stringsAsFactors = FALSE) -y <- sapply(wage,class) -z <- colnames(wage) +file <- read.csv(file="wages.csv", header = TRUE, stringsAsFactors = FALSE) +y <- sapply(file,class) +z <- colnames(file) print(paste("Which column of wages.csv would you like to calculate the coefficient of variation for? The columns are" , z , "/" ,y)) print("Please only select a column of integers or numerics.") x <- as.name(readline()) -mean <- mean(wage[[x]]) -std <- sd(wage[[x]]) +mean <- mean(file[[x]]) +std <- sd(file[[x]]) cov <- std/mean fileCOV <- c(fileCOV, cov) From 1edd802e01f8e2b5e5a7520f56c02c066bb51266 Mon Sep 17 00:00:00 2001 From: Megan Schmidt Date: Thu, 18 Nov 2021 12:13:30 -0500 Subject: [PATCH 3/4] Add 50 if-else --- Tutorial9.R | 58 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/Tutorial9.R b/Tutorial9.R index 6a5e520..36bde0d 100644 --- a/Tutorial9.R +++ b/Tutorial9.R @@ -2,38 +2,82 @@ setwd("C:/Users/megan/Desktop/r-novice-inflammation/r-novice-inflammation-data/d dir <- list.files("../data", full.names = TRUE) dir fileCOV <- c() - +usable <- c() coeffientOfVariation <- function(dir){ csv <- dir[grep(".csv",dir)] table <- dir[grep(".txt",dir)] usable <- c(csv,table) for (i in 1:length(usable)){ - if (usable[grep(".csv", usable)] == TRUE){ fil <- read.csv(file = usable[i], header = TRUE, stringsAsFactors = FALSE) - } - else { - fil <- read.table(file = usable[i], sep = ,header = TRUE, stringsAsFactors = FALSE) - } y <- sapply(fil,class) z <- colnames(fil) print(paste("Which column of the file would you like to calculate the coefficient of variation for? The columns are" , z , "/" ,y)) print("Please only select a column of integers or numerics.") x <- as.name(readline()) + if (length(fil[[x]]) < 50){ + print("An accurate coefficient of variation should have at least 50 entries, and this column has less than 50.") + question <- (readline("Would you like to continue? Yes or No")) + if (question == "YES"){ + mean <- mean(fil[[x]]) + std <- sd(fil[[x]]) + cov <- std/mean + fileCOV <- c(fileCOV, cov) + i = i + 1 + }else { + i = i +1 + } + } + else { mean <- mean(fil[[x]]) std <- sd(fil[[x]]) cov <- std/mean fileCOV <- c(fileCOV, cov) i = i + 1 + } } } -file <- read.csv(file="wages.csv", header = TRUE, stringsAsFactors = FALSE) +coeffientOfVariation(dir) + + + +file <- read.csv(file="car-speeds.csv", header = TRUE, stringsAsFactors = FALSE) y <- sapply(file,class) z <- colnames(file) print(paste("Which column of wages.csv would you like to calculate the coefficient of variation for? The columns are" , z , "/" ,y)) print("Please only select a column of integers or numerics.") x <- as.name(readline()) +if (length(file[[x]]) < 50){ + print("An accurate coefficient of variation should have at least 50 entries, and this column has less than 50.") + question <- (readline("Would you like to continue? Yes or No")) + if (question == "YES"){ + mean <- mean(file[[x]]) + std <- sd(file[[x]]) + cov <- std/mean + fileCOV <- c(fileCOV, cov) + }else{ + print("Okay") + } +}else { + mean <- mean(file[[x]]) + std <- sd(file[[x]]) + cov <- std/mean + fileCOV <- c(fileCOV, cov) +} + + +length(file$wage) + mean <- mean(file[[x]]) std <- sd(file[[x]]) cov <- std/mean fileCOV <- c(fileCOV, cov) + +if (question == "YES"){ + mean <- mean(file[[x]]) + std <- sd(file[[x]]) + cov <- std/mean + fileCOV <- c(fileCOV, cov) +}else{ + print("Okay") +} \ No newline at end of file From 9c42f5d26123efdc55a6612c88542daf80858ab3 Mon Sep 17 00:00:00 2001 From: Megan Schmidt Date: Thu, 18 Nov 2021 19:33:34 -0500 Subject: [PATCH 4/4] done --- Tutorial9.R | 82 +++++++++++++++++++---------------------------------- 1 file changed, 29 insertions(+), 53 deletions(-) diff --git a/Tutorial9.R b/Tutorial9.R index 36bde0d..41762bc 100644 --- a/Tutorial9.R +++ b/Tutorial9.R @@ -1,83 +1,59 @@ setwd("C:/Users/megan/Desktop/r-novice-inflammation/r-novice-inflammation-data/data") +#list of files dir <- list.files("../data", full.names = TRUE) dir +#return vector fileCOV <- c() +#only csv files vector usable <- c() coeffientOfVariation <- function(dir){ - csv <- dir[grep(".csv",dir)] - table <- dir[grep(".txt",dir)] - usable <- c(csv,table) + #only csv files + usable <- dir[grep(".csv", dir)] + #for number of csv files for (i in 1:length(usable)){ + #read file fil <- read.csv(file = usable[i], header = TRUE, stringsAsFactors = FALSE) + #column type y <- sapply(fil,class) + #column names z <- colnames(fil) print(paste("Which column of the file would you like to calculate the coefficient of variation for? The columns are" , z , "/" ,y)) print("Please only select a column of integers or numerics.") + #input from user x <- as.name(readline()) + #test if the column has more than 50 entries if (length(fil[[x]]) < 50){ print("An accurate coefficient of variation should have at least 50 entries, and this column has less than 50.") - question <- (readline("Would you like to continue? Yes or No")) - if (question == "YES"){ - mean <- mean(fil[[x]]) - std <- sd(fil[[x]]) + question <- (readline("Would you like to continue? Yes or No: ")) + #if less than 50 entries ask user if they want to continue + if (question == "Yes"){ + #mean and removes NA occurrences + mean <- mean(fil[[x]], na.rm = TRUE) + #standard deviation and removes NA occurrences + std <- sd(fil[[x]], na.rm = TRUE) + #coefficient of variation cov <- std/mean + #return vector fileCOV <- c(fileCOV, cov) i = i + 1 }else { i = i +1 } } + #if over 50 entries else { - mean <- mean(fil[[x]]) - std <- sd(fil[[x]]) + #mean and removes NA occurrences + mean <- mean(fil[[x]], na.rm = TRUE) + #standard deviation and removes NA occurrences + std <- sd(fil[[x]], na.rm = TRUE) + #coefficient of variation cov <- std/mean + #return vector fileCOV <- c(fileCOV, cov) i = i + 1 } } - + #actual returning of vector + return(fileCOV) } coeffientOfVariation(dir) - - - -file <- read.csv(file="car-speeds.csv", header = TRUE, stringsAsFactors = FALSE) -y <- sapply(file,class) -z <- colnames(file) -print(paste("Which column of wages.csv would you like to calculate the coefficient of variation for? The columns are" , z , "/" ,y)) -print("Please only select a column of integers or numerics.") -x <- as.name(readline()) -if (length(file[[x]]) < 50){ - print("An accurate coefficient of variation should have at least 50 entries, and this column has less than 50.") - question <- (readline("Would you like to continue? Yes or No")) - if (question == "YES"){ - mean <- mean(file[[x]]) - std <- sd(file[[x]]) - cov <- std/mean - fileCOV <- c(fileCOV, cov) - }else{ - print("Okay") - } -}else { - mean <- mean(file[[x]]) - std <- sd(file[[x]]) - cov <- std/mean - fileCOV <- c(fileCOV, cov) -} - - -length(file$wage) - -mean <- mean(file[[x]]) -std <- sd(file[[x]]) -cov <- std/mean -fileCOV <- c(fileCOV, cov) - -if (question == "YES"){ - mean <- mean(file[[x]]) - std <- sd(file[[x]]) - cov <- std/mean - fileCOV <- c(fileCOV, cov) -}else{ - print("Okay") -} \ No newline at end of file