From 14cd978a07bcf0324485fcc2529399f40d590b2b Mon Sep 17 00:00:00 2001 From: Johanna1403 Date: Tue, 16 Nov 2021 21:53:14 -0500 Subject: [PATCH 1/5] Initial commit. Needs debugging --- Exercise9.R | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Exercise9.R diff --git a/Exercise9.R b/Exercise9.R new file mode 100644 index 0000000..b70ef5d --- /dev/null +++ b/Exercise9.R @@ -0,0 +1,48 @@ +# Exercise 9 +# here it is assumed that all the files are CSV files (although the user can specify the delimiter type) +# works only if the directory contains files that have the same separator (here the user can choose between comma or tab separated files) + + +VarCoefficient <- function(dir, column, observations = 0) { + setwd(dir) + list_files = list.files(path = dir) + variations = NULL + for (f in 1:length(list_files)) { + data <- read.csv(file = list_files[f], header = T, sep = ",", stringsAsFactors = FALSE) + obs = nrow(data) + if (observations == 0){ + if (obs < 50) { + print("Error! Too few observations!") + next + } + else { + sd_col = sd(data[, column]) + mean_col = mean(data[, column]) + variationCoeff = sd_col / mean_col + variations = c(variations, variationCoeff) + } + } + else if (observations == 1) { + if (obs < 50) { + print("Warning! Less than 50 observations.") + sd_col = sd(data[, column]) + mean_col = mean(data[, column]) + variationCoeff = sd_col / mean_col + variations = c(variations, variationCoeff) + } + else { + sd_col = sd(data[, column]) + mean_col = mean(data[, column]) + variationCoeff = sd_col / mean_col + variations = c(variations, variationCoeff) + } + } + } + return(variations) +} + +VarCoefficient("/Users/johi/Desktop/Courses/Biocomputing/r-novice-inflammation/infl_data", 2, observations = 1) + + + + From e79db0fc6ed0fadfeb2301e18e284521efbca637 Mon Sep 17 00:00:00 2001 From: Johanna1403 Date: Tue, 16 Nov 2021 23:27:22 -0500 Subject: [PATCH 2/5] Debugged and implemented code. --- Exercise9.R | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/Exercise9.R b/Exercise9.R index b70ef5d..3419204 100644 --- a/Exercise9.R +++ b/Exercise9.R @@ -1,14 +1,15 @@ # Exercise 9 # here it is assumed that all the files are CSV files (although the user can specify the delimiter type) -# works only if the directory contains files that have the same separator (here the user can choose between comma or tab separated files) +# works only if the directory contains files that have the same separator -VarCoefficient <- function(dir, column, observations = 0) { + +VarCoefficient <- function(dir, column, observations = 0, delimiter = ",") { setwd(dir) list_files = list.files(path = dir) variations = NULL for (f in 1:length(list_files)) { - data <- read.csv(file = list_files[f], header = T, sep = ",", stringsAsFactors = FALSE) + data <- read.csv(file = list_files[f], header = T, sep = delimiter, stringsAsFactors = FALSE) obs = nrow(data) if (observations == 0){ if (obs < 50) { @@ -16,33 +17,22 @@ VarCoefficient <- function(dir, column, observations = 0) { next } else { - sd_col = sd(data[, column]) - mean_col = mean(data[, column]) - variationCoeff = sd_col / mean_col - variations = c(variations, variationCoeff) } } else if (observations == 1) { if (obs < 50) { print("Warning! Less than 50 observations.") - sd_col = sd(data[, column]) - mean_col = mean(data[, column]) - variationCoeff = sd_col / mean_col - variations = c(variations, variationCoeff) } else { - sd_col = sd(data[, column]) - mean_col = mean(data[, column]) - variationCoeff = sd_col / mean_col - variations = c(variations, variationCoeff) } } + sd_col = sd(data[, column]) + mean_col = mean(data[, column]) + variationCoeff = sd_col / mean_col + variations = c(variations, variationCoeff) } return(variations) } -VarCoefficient("/Users/johi/Desktop/Courses/Biocomputing/r-novice-inflammation/infl_data", 2, observations = 1) - - - +VarCoefficient("/Users/johi/Desktop/Courses/Biocomputing/r-novice-inflammation/infl_data", 5, observations = 0) From b8d575fbc109dc1dd00cf4842fdd7ce983d431fd Mon Sep 17 00:00:00 2001 From: Johanna1403 Date: Thu, 18 Nov 2021 10:39:35 -0500 Subject: [PATCH 3/5] Final implementations, comments and usage added. --- Exercise9.R | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Exercise9.R b/Exercise9.R index 3419204..3090e4f 100644 --- a/Exercise9.R +++ b/Exercise9.R @@ -1,7 +1,13 @@ # Exercise 9 -# here it is assumed that all the files are CSV files (although the user can specify the delimiter type) -# works only if the directory contains files that have the same separator +# Here it is assumed that all the files are CSV files (although the user can specify the delimiter type) +# Works only if the directory contains files that have the same separator + +# USAGE: VarCoefficient(dir, column, observations = 0, delimiter = ",") +# dir is the user specified directory +# column is the user specified column in each file in the directory +# observations (default is 0) specifies if the function prints out an error when encountering file with less than 50 observations (user can set the observations argument equal to 1, in that case the error message is overrun and only warning message is given) +# delimiter allows the user to specify the file separator type (default is comma separated, but user could also specify tab separated). VarCoefficient <- function(dir, column, observations = 0, delimiter = ",") { @@ -9,7 +15,7 @@ VarCoefficient <- function(dir, column, observations = 0, delimiter = ",") { list_files = list.files(path = dir) variations = NULL for (f in 1:length(list_files)) { - data <- read.csv(file = list_files[f], header = T, sep = delimiter, stringsAsFactors = FALSE) + data <- read.table(file = list_files[f], header = T, sep = delimiter, stringsAsFactors = FALSE) obs = nrow(data) if (observations == 0){ if (obs < 50) { @@ -26,13 +32,11 @@ VarCoefficient <- function(dir, column, observations = 0, delimiter = ",") { else { } } - sd_col = sd(data[, column]) - mean_col = mean(data[, column]) - variationCoeff = sd_col / mean_col + variationCoeff = sd(data[, column]) / mean(data[, column]) variations = c(variations, variationCoeff) } return(variations) } -VarCoefficient("/Users/johi/Desktop/Courses/Biocomputing/r-novice-inflammation/infl_data", 5, observations = 0) +VarCoefficient("/Users/johi/Desktop/Courses/Biocomputing/r-novice-inflammation/infl_data", 5, observations = 1) From ed1f2593048113e0f485c8991d9d52388dcb46fc Mon Sep 17 00:00:00 2001 From: Johanna1403 <69310080+Johanna1403@users.noreply.github.com> Date: Thu, 18 Nov 2021 10:46:04 -0500 Subject: [PATCH 4/5] Delete Exercise9.R --- Exercise9.R | 42 ------------------------------------------ 1 file changed, 42 deletions(-) delete mode 100644 Exercise9.R diff --git a/Exercise9.R b/Exercise9.R deleted file mode 100644 index 3090e4f..0000000 --- a/Exercise9.R +++ /dev/null @@ -1,42 +0,0 @@ -# Exercise 9 - -# Here it is assumed that all the files are CSV files (although the user can specify the delimiter type) -# Works only if the directory contains files that have the same separator - -# USAGE: VarCoefficient(dir, column, observations = 0, delimiter = ",") -# dir is the user specified directory -# column is the user specified column in each file in the directory -# observations (default is 0) specifies if the function prints out an error when encountering file with less than 50 observations (user can set the observations argument equal to 1, in that case the error message is overrun and only warning message is given) -# delimiter allows the user to specify the file separator type (default is comma separated, but user could also specify tab separated). - - -VarCoefficient <- function(dir, column, observations = 0, delimiter = ",") { - setwd(dir) - list_files = list.files(path = dir) - variations = NULL - for (f in 1:length(list_files)) { - data <- read.table(file = list_files[f], header = T, sep = delimiter, stringsAsFactors = FALSE) - obs = nrow(data) - if (observations == 0){ - if (obs < 50) { - print("Error! Too few observations!") - next - } - else { - } - } - else if (observations == 1) { - if (obs < 50) { - print("Warning! Less than 50 observations.") - } - else { - } - } - variationCoeff = sd(data[, column]) / mean(data[, column]) - variations = c(variations, variationCoeff) - } - return(variations) -} - -VarCoefficient("/Users/johi/Desktop/Courses/Biocomputing/r-novice-inflammation/infl_data", 5, observations = 1) - From 07680312a7c7c754d790ab30b52705d0b3b8e2fd Mon Sep 17 00:00:00 2001 From: Johanna1403 Date: Thu, 18 Nov 2021 11:05:23 -0500 Subject: [PATCH 5/5] Restored file --- Exercise9.R | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Exercise9.R diff --git a/Exercise9.R b/Exercise9.R new file mode 100644 index 0000000..065f2f1 --- /dev/null +++ b/Exercise9.R @@ -0,0 +1,40 @@ +# Exercise 9 + +# Here it is assumed that all the files are CSV files (although the user can specify the delimiter type) +# Works only if the directory contains files that have the same separator + +# USAGE: VarCoefficient(dir, column, observations = 0, delimiter = ",") +# dir is the user specified directory +# column is the user specified column in each file in the directory +# observations (default is 0) specifies if the function prints out an error when encountering file with less than 50 observations (user can set the observations argument equal to 1, in that case the error message is overrun and only warning message is given) +# delimiter allows the user to specify the file separator type (default is comma separated, but user could also specify tab separated). + +VarCoefficient <- function(dir, column, observations = 0, delimiter = ",") { + setwd(dir) + list_files = list.files(path = dir) + variations = NULL + for (f in 1:length(list_files)) { + data <- read.table(file = list_files[f], header = T, sep = delimiter, stringsAsFactors = FALSE) + obs = nrow(data) + if (observations == 0){ + if (obs < 50) { + print("Error! Too few observations!") + next + } + else { + } + } + else if (observations == 1) { + if (obs < 50) { + print("Warning! Less than 50 observations.") + } + else { + } + } + variationCoeff = sd(data[, column]) / mean(data[, column]) + variations = c(variations, variationCoeff) + } + return(variations) +} + +VarCoefficient("/Users/johi/Desktop/Courses/Biocomputing/r-novice-inflammation/infl_data", 5, observations = 0) \ No newline at end of file