From 4bc2e6d6a0619d24b786c686c9c0992278851041 Mon Sep 17 00:00:00 2001 From: Nicholas Ross Date: Thu, 11 Nov 2021 21:02:51 -0500 Subject: [PATCH 1/3] Homework --- Ex_08.R | 59 +++++++++++++++++++ GuessingGame.R | 26 +++++++++ Tut10.R | 24 ++++++++ iris.csv | 151 ------------------------------------------------- 4 files changed, 109 insertions(+), 151 deletions(-) create mode 100644 Ex_08.R create mode 100644 GuessingGame.R create mode 100644 Tut10.R delete mode 100644 iris.csv diff --git a/Ex_08.R b/Ex_08.R new file mode 100644 index 0000000..1e0a7d2 --- /dev/null +++ b/Ex_08.R @@ -0,0 +1,59 @@ +## read in table with score data +scores = read.table("UWvMSU_1-22-13.txt", sep = "\t", header = T, stringsAsFactors = F) + +## declare all variable to be used +wisconsin = 0 +msu = 0 + +scoreUW = 0 +scoreMSU = 0 + +## set up for loop to run through all rows of scores table +for (i in 1:nrow(scores)){ + ## if team column is UW then put the scores into a new vector + if(scores$team[i] == "UW"){ + scoreUW [i] = wisconsin + scores$score[i] + wisconsin = scoreUW[i] + scoreMSU [i] = msu + } else{ + scoreMSU [i] = msu + scores$score[i] + msu = scoreMSU[i] + scoreUW[i] = wisconsin + } +} +## plot the functions above +plot(scores$time, wisconsin, + main = "UW/MSU Scores vs Time", + ylab = "Scores", + xlab = "Time (min)", + type = "l", + col = "red") +grid(NULL,NULL) +lines(scores$time, msu, col="green") +legend("topleft", + c("UW", "MSU"), + fill = c("red","green")) + + +### Guessing Number Game ### + +## generate random number that the user will try to guess what it is +magicNum = sample(1:100,1, replace = TRUE) + +print("Welcome the Guessing Game. You will have 10 chances to guess the number (1-100) I am thinking of. Are you ready? Here we go.") +counter = 1 + +while (counter <= 10) { + guess = readline(prompt ="Please enter your guess: ") + if (guess == magicNum){ + print("WOW WEE WOW WOO WOW! You got it right! Congrats!") + options("show.error.messages" = F) + stop() + } else if (guess < magicNum){ + print(paste0("The number you guessed is lower than the magic number. You have ", 10-counter, " attempts left.")) + counter = counter + 1 + } else if (guess > magicNum) { + print(paste0("The number you guessed is higher than the magic number. You have ", 10-counter, " attempts left.")) + counter = counter + 1 + } +} diff --git a/GuessingGame.R b/GuessingGame.R new file mode 100644 index 0000000..e1b93ca --- /dev/null +++ b/GuessingGame.R @@ -0,0 +1,26 @@ +### Guessing Number Game ### + +## generate random number that the user will try to guess what it is +magicNum = sample(1:100,1, replace = TRUE) + +print("Welcome the Guessing Game. You will have 10 chances to guess the number I am thinking of. Are you ready? Here we go.") +guess = readline(prompt ="Please enter your guess: ") + +counter = 0 + +while (counter < 11) { + if (guess == magicNum){ + print("WOW WEE WOW WOO WOW! You got it right! Congrats!") + options("show.error.messages" = F) + stop() + } else if (guess < magicNum){ + counter = counter + 1 + print(paste0("The number you guessed is lower than the magic number. You have ", 10-counter, " attempts left.")) + guess = readline(prompt="The number you guessed is lower than the magic number. Try again: ") + } else if (guess > magicNum) { + counter = counter + 1 + print(paste0("The number you guessed is higher than the magic number. You have ", 10-counter, " attempts left.")) + guess = readline(prompt="Try again: ") + } +} +print(paste0("Unfortunately, you're out of guesses. The correct number was ", magicNum)) diff --git a/Tut10.R b/Tut10.R new file mode 100644 index 0000000..1676310 --- /dev/null +++ b/Tut10.R @@ -0,0 +1,24 @@ +dt = data.frame(a = c(1:4), + b = c(5:8), + c = c(9:12)) + +for (i in 1:length(dt$a)){ + row = dt[i,] + print(row) + + col.3 = dt$c[i] + print(col.3) +} + +wages = read.csv("wages.csv", stringsAsFactors = F, header = T) +maleWage = 0 +femaleWage = 0 +for (i in 1:length(wages$gender)){ + if (wages$gender[i] == 'male'){ + maleWage = maleWage + wages$wage[i] + } else { + femaleWage = femaleWage + wages$wage[i] + } +} +print(maleWage) +print(femaleWage) \ No newline at end of file diff --git a/iris.csv b/iris.csv deleted file mode 100644 index 08f772c..0000000 --- a/iris.csv +++ /dev/null @@ -1,151 +0,0 @@ -"Sepal.Length","Sepal.Width","Petal.Length","Petal.Width","Species" -5.1,3.5,1.4,0.2,"setosa" -4.9,3,1.4,0.2,"setosa" -4.7,3.2,1.3,0.2,"setosa" -4.6,3.1,1.5,0.2,"setosa" -5,3.6,1.4,0.2,"setosa" -5.4,3.9,1.7,0.4,"setosa" -4.6,3.4,1.4,0.3,"setosa" -5,3.4,1.5,0.2,"setosa" -4.4,2.9,1.4,0.2,"setosa" -4.9,3.1,1.5,0.1,"setosa" -5.4,3.7,1.5,0.2,"setosa" -4.8,3.4,1.6,0.2,"setosa" -4.8,3,1.4,0.1,"setosa" -4.3,3,1.1,0.1,"setosa" -5.8,4,1.2,0.2,"setosa" -5.7,4.4,1.5,0.4,"setosa" -5.4,3.9,1.3,0.4,"setosa" -5.1,3.5,1.4,0.3,"setosa" -5.7,3.8,1.7,0.3,"setosa" -5.1,3.8,1.5,0.3,"setosa" -5.4,3.4,1.7,0.2,"setosa" -5.1,3.7,1.5,0.4,"setosa" -4.6,3.6,1,0.2,"setosa" -5.1,3.3,1.7,0.5,"setosa" -4.8,3.4,1.9,0.2,"setosa" -5,3,1.6,0.2,"setosa" -5,3.4,1.6,0.4,"setosa" -5.2,3.5,1.5,0.2,"setosa" -5.2,3.4,1.4,0.2,"setosa" -4.7,3.2,1.6,0.2,"setosa" -4.8,3.1,1.6,0.2,"setosa" -5.4,3.4,1.5,0.4,"setosa" -5.2,4.1,1.5,0.1,"setosa" -5.5,4.2,1.4,0.2,"setosa" -4.9,3.1,1.5,0.2,"setosa" -5,3.2,1.2,0.2,"setosa" -5.5,3.5,1.3,0.2,"setosa" -4.9,3.6,1.4,0.1,"setosa" -4.4,3,1.3,0.2,"setosa" -5.1,3.4,1.5,0.2,"setosa" -5,3.5,1.3,0.3,"setosa" -4.5,2.3,1.3,0.3,"setosa" -4.4,3.2,1.3,0.2,"setosa" -5,3.5,1.6,0.6,"setosa" -5.1,3.8,1.9,0.4,"setosa" -4.8,3,1.4,0.3,"setosa" -5.1,3.8,1.6,0.2,"setosa" -4.6,3.2,1.4,0.2,"setosa" -5.3,3.7,1.5,0.2,"setosa" -5,3.3,1.4,0.2,"setosa" -7,3.2,4.7,1.4,"versicolor" -6.4,3.2,4.5,1.5,"versicolor" -6.9,3.1,4.9,1.5,"versicolor" -5.5,2.3,4,1.3,"versicolor" -6.5,2.8,4.6,1.5,"versicolor" -5.7,2.8,4.5,1.3,"versicolor" -6.3,3.3,4.7,1.6,"versicolor" -4.9,2.4,3.3,1,"versicolor" -6.6,2.9,4.6,1.3,"versicolor" -5.2,2.7,3.9,1.4,"versicolor" -5,2,3.5,1,"versicolor" -5.9,3,4.2,1.5,"versicolor" -6,2.2,4,1,"versicolor" -6.1,2.9,4.7,1.4,"versicolor" -5.6,2.9,3.6,1.3,"versicolor" -6.7,3.1,4.4,1.4,"versicolor" -5.6,3,4.5,1.5,"versicolor" -5.8,2.7,4.1,1,"versicolor" -6.2,2.2,4.5,1.5,"versicolor" -5.6,2.5,3.9,1.1,"versicolor" -5.9,3.2,4.8,1.8,"versicolor" -6.1,2.8,4,1.3,"versicolor" -6.3,2.5,4.9,1.5,"versicolor" -6.1,2.8,4.7,1.2,"versicolor" -6.4,2.9,4.3,1.3,"versicolor" -6.6,3,4.4,1.4,"versicolor" -6.8,2.8,4.8,1.4,"versicolor" -6.7,3,5,1.7,"versicolor" -6,2.9,4.5,1.5,"versicolor" -5.7,2.6,3.5,1,"versicolor" -5.5,2.4,3.8,1.1,"versicolor" -5.5,2.4,3.7,1,"versicolor" -5.8,2.7,3.9,1.2,"versicolor" -6,2.7,5.1,1.6,"versicolor" -5.4,3,4.5,1.5,"versicolor" -6,3.4,4.5,1.6,"versicolor" -6.7,3.1,4.7,1.5,"versicolor" -6.3,2.3,4.4,1.3,"versicolor" -5.6,3,4.1,1.3,"versicolor" -5.5,2.5,4,1.3,"versicolor" -5.5,2.6,4.4,1.2,"versicolor" -6.1,3,4.6,1.4,"versicolor" -5.8,2.6,4,1.2,"versicolor" -5,2.3,3.3,1,"versicolor" -5.6,2.7,4.2,1.3,"versicolor" -5.7,3,4.2,1.2,"versicolor" -5.7,2.9,4.2,1.3,"versicolor" -6.2,2.9,4.3,1.3,"versicolor" -5.1,2.5,3,1.1,"versicolor" -5.7,2.8,4.1,1.3,"versicolor" -6.3,3.3,6,2.5,"virginica" -5.8,2.7,5.1,1.9,"virginica" -7.1,3,5.9,2.1,"virginica" -6.3,2.9,5.6,1.8,"virginica" -6.5,3,5.8,2.2,"virginica" -7.6,3,6.6,2.1,"virginica" -4.9,2.5,4.5,1.7,"virginica" -7.3,2.9,6.3,1.8,"virginica" -6.7,2.5,5.8,1.8,"virginica" -7.2,3.6,6.1,2.5,"virginica" -6.5,3.2,5.1,2,"virginica" -6.4,2.7,5.3,1.9,"virginica" -6.8,3,5.5,2.1,"virginica" -5.7,2.5,5,2,"virginica" -5.8,2.8,5.1,2.4,"virginica" -6.4,3.2,5.3,2.3,"virginica" -6.5,3,5.5,1.8,"virginica" -7.7,3.8,6.7,2.2,"virginica" -7.7,2.6,6.9,2.3,"virginica" -6,2.2,5,1.5,"virginica" -6.9,3.2,5.7,2.3,"virginica" -5.6,2.8,4.9,2,"virginica" -7.7,2.8,6.7,2,"virginica" -6.3,2.7,4.9,1.8,"virginica" -6.7,3.3,5.7,2.1,"virginica" -7.2,3.2,6,1.8,"virginica" -6.2,2.8,4.8,1.8,"virginica" -6.1,3,4.9,1.8,"virginica" -6.4,2.8,5.6,2.1,"virginica" -7.2,3,5.8,1.6,"virginica" -7.4,2.8,6.1,1.9,"virginica" -7.9,3.8,6.4,2,"virginica" -6.4,2.8,5.6,2.2,"virginica" -6.3,2.8,5.1,1.5,"virginica" -6.1,2.6,5.6,1.4,"virginica" -7.7,3,6.1,2.3,"virginica" -6.3,3.4,5.6,2.4,"virginica" -6.4,3.1,5.5,1.8,"virginica" -6,3,4.8,1.8,"virginica" -6.9,3.1,5.4,2.1,"virginica" -6.7,3.1,5.6,2.4,"virginica" -6.9,3.1,5.1,2.3,"virginica" -5.8,2.7,5.1,1.9,"virginica" -6.8,3.2,5.9,2.3,"virginica" -6.7,3.3,5.7,2.5,"virginica" -6.7,3,5.2,2.3,"virginica" -6.3,2.5,5,1.9,"virginica" -6.5,3,5.2,2,"virginica" -6.2,3.4,5.4,2.3,"virginica" -5.9,3,5.1,1.8,"virginica" From 1e2a91dd76f69078189192c2b3be337a557ca274 Mon Sep 17 00:00:00 2001 From: Nicholas Ross Date: Thu, 11 Nov 2021 21:07:50 -0500 Subject: [PATCH 2/3] deleted file --- GuessingGame.R | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 GuessingGame.R diff --git a/GuessingGame.R b/GuessingGame.R deleted file mode 100644 index e1b93ca..0000000 --- a/GuessingGame.R +++ /dev/null @@ -1,26 +0,0 @@ -### Guessing Number Game ### - -## generate random number that the user will try to guess what it is -magicNum = sample(1:100,1, replace = TRUE) - -print("Welcome the Guessing Game. You will have 10 chances to guess the number I am thinking of. Are you ready? Here we go.") -guess = readline(prompt ="Please enter your guess: ") - -counter = 0 - -while (counter < 11) { - if (guess == magicNum){ - print("WOW WEE WOW WOO WOW! You got it right! Congrats!") - options("show.error.messages" = F) - stop() - } else if (guess < magicNum){ - counter = counter + 1 - print(paste0("The number you guessed is lower than the magic number. You have ", 10-counter, " attempts left.")) - guess = readline(prompt="The number you guessed is lower than the magic number. Try again: ") - } else if (guess > magicNum) { - counter = counter + 1 - print(paste0("The number you guessed is higher than the magic number. You have ", 10-counter, " attempts left.")) - guess = readline(prompt="Try again: ") - } -} -print(paste0("Unfortunately, you're out of guesses. The correct number was ", magicNum)) From a0e1cde3879295460679c61a4d8e3ed86237a813 Mon Sep 17 00:00:00 2001 From: Nicholas Ross Date: Thu, 11 Nov 2021 21:19:46 -0500 Subject: [PATCH 3/3] Added comments to guessing game --- Ex_08.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Ex_08.R b/Ex_08.R index 1e0a7d2..59e3221 100644 --- a/Ex_08.R +++ b/Ex_08.R @@ -40,11 +40,17 @@ legend("topleft", ## generate random number that the user will try to guess what it is magicNum = sample(1:100,1, replace = TRUE) +## tell the user about the guessing game print("Welcome the Guessing Game. You will have 10 chances to guess the number (1-100) I am thinking of. Are you ready? Here we go.") + +## set counter for guess attempts counter = 1 +## create while loop to end after 10 guess attempts have been made or correct answer while (counter <= 10) { + ## have user input guess guess = readline(prompt ="Please enter your guess: ") + ## set if else statements for when user guesses right, too high, or too low if (guess == magicNum){ print("WOW WEE WOW WOO WOW! You got it right! Congrats!") options("show.error.messages" = F)