Skip to content
Open
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions Exercise09.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## Exercise 9 ##
## Pulling al of the R programming pieces together ##

#Create function to calculate coeffecient variance of separate files
covar = function(dir, colNo){
covs = 0
path = list.files(dir) ##list all the files in the directory

# read through each file in directory
for (i in 1:length(path)){
file = paste0(dir,path[i]) ## create a full path name to read in the file in the next line
info = read.table(file, header = T, sep=",", stringsAsFactors = F) ## read in tabular data

#see if file has less than 50 rows/observations and warn user if so
if (length(info[,colNo]) < 50){
print(paste0("Warning: file ", file[i]," has less 50 oberservations"))

#take in the column specified by the user and calculate coefficient of variation
covs [i]= (sd(info[,colNo])/mean(info[,colNo]))*100

} else {

#take in the column specified by the user and calculate coefficient of variation
covs [i]= (sd(info[,colNo])/mean(info[,colNo]))*100
}
}
return(covs) #return the coeffecient of variance values from each file
}

print("Enter the directory in which you would like to read files from. Be sure to include a '/' at the end of the directory name.")
userDir = readline(prompt = "Directory: ")
fileCol = as.integer(readline(prompt = "Which column do you want to find the coeffecient of variance for? "))
covar(userDir, fileCol) # call function
42 changes: 42 additions & 0 deletions lessthan50.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
gender,yearsExperience,yearsSchool,wage,,STD,AVG,COV
female,39,42,6.315295646,EXP,16.94704809,21.56097561,78.60056242
female,29,16,5.479769979,SCHOOL,15.62857764,25.43902439,61.43544422
female,43,37,3.642169917,,,,
female,22,17,4.5933366,,,,
female,21,42,2.418157461,,,,
female,31,16,2.09405811,,,,
female,10,1,5.51200392,,,,
female,6,45,3.54842716,,,,
female,5,7,5.81822636,,,,
female,47,10,3.8277805,,,,
female,48,13,6.73689368,,,,
female,30,20,12.86134248,,,,
female,5,0,7.655561,,,,
female,20,2,2.44977952,,,,
female,3,36,6.105662147,,,,
female,2,40,8.26800588,,,,
female,33,44,1.862405483,,,,
female,2,47,3.98089172,,,,
female,1,30,4.763460178,,,,
female,7,8,6.380595586,,,,
female,48,2,5.631123847,,,,
female,44,10,2.488057325,,,,
female,16,22,1.223738277,,,,
female,16,18,1.345653539,,,,
female,1,33,0.751111645,,,,
female,47,39,11.33023028,,,,
female,3,22,3.894757656,,,,
female,13,23,2.998515251,,,,
female,18,29,3.267896188,,,,
female,11,7,1.355197181,,,,
female,42,6,4.28711416,,,,
female,10,37,7.624938756,,,,
female,3,44,4.443786106,,,,
female,13,28,1.134281476,,,,
female,42,44,9.064184223,,,,
female,19,43,3.98089172,,,,
female,2,42,3.450705238,,,,
female,33,38,8.096728922,,,,
female,49,37,8.735213831,,,,
female,5,44,7.275845174,,,,
female,45,2,5.51200392,,,,
Loading