Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Exercise09.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
### Biocomputing Exercise09 - Pulling the R pieces together

# Defining the function
CoefficientofVariation <- function(dir,col,nrow=50){

# Set a working directory and create the Coefficient of Variation vector
setwd(dir)
inputfiles<-list.files(dir)
coefficientofvariationVector <- numeric(0)

# Sets up a loop for the files in the directory to calculate the coefficient of variation
for (i in inputfiles){
i <- read.csv(i,header=TRUE,sep=",",stringsAsFactors=FALSE, fill=TRUE)
# Checking if the file has the correct number of columns
if(ncol(i)< col){
print("Invalid column number.")
}else{
# Checking if the column has 50 observations
if(nrow(i) <50){
# Offers the override option if the column does not have 50 observations
not50 <- print("Recommended that the file has at least 50 observations to calculate a coefficient of variation. Would you like to proceed? Type 'Y' for Yes and 'N' for No: ")
if(not50=="Y"){
mean=mean(i[,col])
standarddeviation=sd(i[,col])
coefficientofvariation=standarddeviation/mean
coefficientofvariationVector=c(coefficientofvariationVector,coefficientofvariation)
}else{
# Output if the user chooses not to override
print("Function stopped.")
}
}else{
mean=mean(i[,col])
standardeviation=sd(i[,col])
coefficientofvariation=standardeviation/mean
coefficientofvariationVector=c(coefficientofvariationVector,coefficientofvariation)
}
}
}

return(coefficientofvariationVector)
}