From ef4e1870ceaa4facf0dc2589e452972db31b636a Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 11:27:41 -0400 Subject: [PATCH 01/17] Adds code to create a random number --- Exercise8Script.R | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Exercise8Script.R diff --git a/Exercise8Script.R b/Exercise8Script.R new file mode 100644 index 0000000..f5186e7 --- /dev/null +++ b/Exercise8Script.R @@ -0,0 +1,8 @@ +###Marlee Shaffer +##Exercise 8 + +###Part 1 +##Generate a plot similar using the data summarized in UWvMSU to show cumulative score throughout the game for University of Wisconsin vs Michigan State University + +df<-read.table("UWvMSU_1-22-13.txt", header=TRUE, sep=" " ) + From 995084b8fbed7b52b3684f71f47e5262206dc5fe Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 11:32:36 -0400 Subject: [PATCH 02/17] Adds the beginning of a for loop --- Exercise8Script.R | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Exercise8Script.R b/Exercise8Script.R index f5186e7..3789a27 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -4,5 +4,10 @@ ###Part 1 ##Generate a plot similar using the data summarized in UWvMSU to show cumulative score throughout the game for University of Wisconsin vs Michigan State University -df<-read.table("UWvMSU_1-22-13.txt", header=TRUE, sep=" " ) +df<-read.table("UWvMSU_1-22-13.txt", header=TRUE, sep=" ") + + +####Part 2 +###Guess my number +rand<- sample(1:100, 1) From 88daf5cf639448006aed001eacebeda9da95bf43 Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 11:48:57 -0400 Subject: [PATCH 03/17] Adds for loop and if else statements for guess my number game --- Exercise8Script.R | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Exercise8Script.R b/Exercise8Script.R index 3789a27..8a9e0a5 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -9,5 +9,30 @@ df<-read.table("UWvMSU_1-22-13.txt", header=TRUE, sep=" ") ####Part 2 ###Guess my number + rand<- sample(1:100, 1) +for (i in 1:10){ + print("I'm thinking of a number 1 to 100.") + usernum <- readline(prompt = "What is your guess?") + if (i < 10){ + if (usernum > rand) { + print("Lower") + } else if (usernum < rand){ + print("Higher") + } else { + print("Correct") + break + } + } + else { + if (usernum == rand){ + print("Correct") + } + else { + print("Sorry. Thanks for playing.") + } + } +} + + From b6e5f9d35fca46396454dbb2ca44c57ff84a8688 Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 11:54:20 -0400 Subject: [PATCH 04/17] Removes quotes from printed words --- Exercise8Script.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Exercise8Script.R b/Exercise8Script.R index 8a9e0a5..57bf53a 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -13,24 +13,24 @@ df<-read.table("UWvMSU_1-22-13.txt", header=TRUE, sep=" ") rand<- sample(1:100, 1) for (i in 1:10){ - print("I'm thinking of a number 1 to 100.") - usernum <- readline(prompt = "What is your guess?") + print("I'm thinking of a number 1 to 100.", quote=FALSE) + usernum <- readline(prompt = "What is your guess? ") if (i < 10){ if (usernum > rand) { - print("Lower") + print("Lower", quote=FALSE) } else if (usernum < rand){ - print("Higher") + print("Higher", quote=FALSE) } else { - print("Correct") + print("Correct", quote=FALSE) break } } else { if (usernum == rand){ - print("Correct") + print("Correct", quote=FALSE) } else { - print("Sorry. Thanks for playing.") + print("Sorry. Thanks for playing.", quote=FALSE) } } } From 46da95313037bbb5d26fbbcfd0dfd42b0ee8e746 Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 11:56:29 -0400 Subject: [PATCH 05/17] fixes problem when random number is 4 and you type 40 and it says higher?? --- Exercise8Script.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exercise8Script.R b/Exercise8Script.R index 57bf53a..657d080 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -14,7 +14,7 @@ rand<- sample(1:100, 1) for (i in 1:10){ print("I'm thinking of a number 1 to 100.", quote=FALSE) - usernum <- readline(prompt = "What is your guess? ") + usernum <- as.integer(readline(prompt = "What is your guess? ")) if (i < 10){ if (usernum > rand) { print("Lower", quote=FALSE) From 0415519929f42d9f2bb991b5579ced885d39b2ea Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 14:41:44 -0400 Subject: [PATCH 06/17] Breaks table into teams and saves them as a variable --- Exercise8Script.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Exercise8Script.R b/Exercise8Script.R index 657d080..fa16aa4 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -4,8 +4,9 @@ ###Part 1 ##Generate a plot similar using the data summarized in UWvMSU to show cumulative score throughout the game for University of Wisconsin vs Michigan State University -df<-read.table("UWvMSU_1-22-13.txt", header=TRUE, sep=" ") - +df<-read.table("UWvMSU_1-22-13.txt", header=TRUE) +UW <- (df[df$team=="UW",]) +MSU <- (df[df$team=="MSU",]) ####Part 2 ###Guess my number @@ -35,4 +36,3 @@ for (i in 1:10){ } } - From a74e2990a8374cd624e7c5716d607dd54ed0ac11 Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 14:47:56 -0400 Subject: [PATCH 07/17] Makes a dataframe for UW data --- Exercise8Script.R | 1 + 1 file changed, 1 insertion(+) diff --git a/Exercise8Script.R b/Exercise8Script.R index fa16aa4..64f13f9 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -8,6 +8,7 @@ df<-read.table("UWvMSU_1-22-13.txt", header=TRUE) UW <- (df[df$team=="UW",]) MSU <- (df[df$team=="MSU",]) + ####Part 2 ###Guess my number From 78aa7a8dc901615bcf2ee278d17bac50aee4417b Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 14:48:41 -0400 Subject: [PATCH 08/17] Fixes that the sum was not cumulative --- Exercise8Script.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exercise8Script.R b/Exercise8Script.R index 64f13f9..b439401 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -7,7 +7,7 @@ df<-read.table("UWvMSU_1-22-13.txt", header=TRUE) UW <- (df[df$team=="UW",]) MSU <- (df[df$team=="MSU",]) - +UWdata <- data.frame(UW$time, sum(UW$score), stringAsFactors = F) ####Part 2 ###Guess my number From 7e71932c085363dda07db45123e3f3344f515d93 Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 14:50:38 -0400 Subject: [PATCH 09/17] Adds dataframe for MSU --- Exercise8Script.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Exercise8Script.R b/Exercise8Script.R index b439401..6d0a316 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -7,7 +7,8 @@ df<-read.table("UWvMSU_1-22-13.txt", header=TRUE) UW <- (df[df$team=="UW",]) MSU <- (df[df$team=="MSU",]) -UWdata <- data.frame(UW$time, sum(UW$score), stringAsFactors = F) +UWdata <- data.frame(UW$time, cumsum(UW$score), stringAsFactors = FALSE) +MSUdata <- data.frame(MSU$time, cumsum(MSU$score), stringAsFactors = FALSE) ####Part 2 ###Guess my number From a453ec4c971113f2fa20e46b111fd93a3052f2fb Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 15:01:26 -0400 Subject: [PATCH 10/17] Adds names to columns of data tables --- Exercise8Script.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Exercise8Script.R b/Exercise8Script.R index 6d0a316..57feb21 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -8,7 +8,9 @@ df<-read.table("UWvMSU_1-22-13.txt", header=TRUE) UW <- (df[df$team=="UW",]) MSU <- (df[df$team=="MSU",]) UWdata <- data.frame(UW$time, cumsum(UW$score), stringAsFactors = FALSE) +names(UWdata)<-c("Time", "Cumulative Score") MSUdata <- data.frame(MSU$time, cumsum(MSU$score), stringAsFactors = FALSE) +names(MSUdata)<-c("Time", "Cumulative Score") ####Part 2 ###Guess my number From 7556442b9aba1f11fd800a96818d1a390debdc07 Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 20:43:26 -0400 Subject: [PATCH 11/17] Well that didn't work as expected so its deleted --- Exercise8Script.R | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Exercise8Script.R b/Exercise8Script.R index 57feb21..1287eaf 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -4,13 +4,6 @@ ###Part 1 ##Generate a plot similar using the data summarized in UWvMSU to show cumulative score throughout the game for University of Wisconsin vs Michigan State University -df<-read.table("UWvMSU_1-22-13.txt", header=TRUE) -UW <- (df[df$team=="UW",]) -MSU <- (df[df$team=="MSU",]) -UWdata <- data.frame(UW$time, cumsum(UW$score), stringAsFactors = FALSE) -names(UWdata)<-c("Time", "Cumulative Score") -MSUdata <- data.frame(MSU$time, cumsum(MSU$score), stringAsFactors = FALSE) -names(MSUdata)<-c("Time", "Cumulative Score") ####Part 2 ###Guess my number From 971a85c6a757d61c057e4f94a5a313f049ad9abb Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 20:55:11 -0400 Subject: [PATCH 12/17] Makes a dataframe from the scores and times --- Exercise8Script.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Exercise8Script.R b/Exercise8Script.R index 1287eaf..f0d348d 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -3,6 +3,8 @@ ###Part 1 ##Generate a plot similar using the data summarized in UWvMSU to show cumulative score throughout the game for University of Wisconsin vs Michigan State University +data<-read.table("UWvMSU_1-22-13.txt", header = TRUE, sep = "\t") +df<-as.data.frame(data) ####Part 2 From 675fe5f8ce4c943593a5edcf9dfea93ab660034b Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 21:03:03 -0400 Subject: [PATCH 13/17] Started to code for a loop following tutorial example --- Exercise8Script.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Exercise8Script.R b/Exercise8Script.R index f0d348d..936ff2e 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -3,8 +3,19 @@ ###Part 1 ##Generate a plot similar using the data summarized in UWvMSU to show cumulative score throughout the game for University of Wisconsin vs Michigan State University +#Load the data and make it a data frame data<-read.table("UWvMSU_1-22-13.txt", header = TRUE, sep = "\t") df<-as.data.frame(data) +#Data frame headers: time, team, score. All data mixed together +#Need to know the number of rows that have to be looped through +rows<-nrow(df) +time<-df$time + +#Make a loop that creates a vector of the scores cumulating over time. Need initial value +UW<-0 +MSU<-0 + + ####Part 2 From 1861a170f1ea58c03fc178dc71945e8382220152 Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 22:02:34 -0400 Subject: [PATCH 14/17] Created a for loop with if-else statements to make a cumulative score vector --- Exercise8Script.R | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Exercise8Script.R b/Exercise8Script.R index 936ff2e..9abe9e8 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -11,11 +11,26 @@ df<-as.data.frame(data) rows<-nrow(df) time<-df$time -#Make a loop that creates a vector of the scores cumulating over time. Need initial value +#Make a loop that creates a vector of the scores accumulating over time. Need initial value UW<-0 MSU<-0 +UWscore<-c(1:rows) +MSUscore<-c(1:rows) +for (i in 1:rows){ + if (df$team[i]=="UW"){ + UW <- UW + df$score[i]} + else if(df$team[i]=="MSU"){ + MSU<- MSU + df$score[i] + UWscore[i]=UW + MSUscore[i]=MSU + } + } + +#Make plot +library(ggplot2) +ggplot(data=df, aes(x="time", y="score")) ####Part 2 From 21d6d076b1e1e1814c3115658a75b98481f355a4 Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Fri, 5 Nov 2021 23:53:29 -0400 Subject: [PATCH 15/17] Got rid of incomplete for loop and replaced with creating vectors and dataframes, ggplot --- Exercise8Script.R | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/Exercise8Script.R b/Exercise8Script.R index 9abe9e8..f617569 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -7,31 +7,22 @@ data<-read.table("UWvMSU_1-22-13.txt", header = TRUE, sep = "\t") df<-as.data.frame(data) #Data frame headers: time, team, score. All data mixed together -#Need to know the number of rows that have to be looped through -rows<-nrow(df) -time<-df$time - -#Make a loop that creates a vector of the scores accumulating over time. Need initial value -UW<-0 -MSU<-0 - -UWscore<-c(1:rows) -MSUscore<-c(1:rows) - -for (i in 1:rows){ - if (df$team[i]=="UW"){ - UW <- UW + df$score[i]} - else if(df$team[i]=="MSU"){ - MSU<- MSU + df$score[i] - UWscore[i]=UW - MSUscore[i]=MSU - } - } +#Pull out data for UW and MSU separately +UWscore <-df[df$team == "UW",] +MSUscore <-df[df$team == "MSU",] +#Vectors for UW +UWtime<-UWscore$time +UWcscore<-cumsum(UWscore$score) +#Vectors for MSU +MSUtime<-MSUscore$time +MSUcscore<-cumsum(MSUscore$score) + +UWdf<-data.frame(UWtime, UWcscore) +MSUdf<-data.frame(MSUtime, MSUcscore) #Make plot library(ggplot2) -ggplot(data=df, aes(x="time", y="score")) - +ggplot()+geom_line(data = UWdf, aes(x=UWtime, y=UWcscore),color="red")+geom_line(data=MSUdf, aes(x=MSUtime, y=MSUcscore), color = "darkgreen")+ ggtitle("Cumulative Score for UW vs MSU Basketball Game")+ xlab("Time")+ylab("Cumulative Score") ####Part 2 ###Guess my number From 85ae31e123db4daa7bcf2e4277d34df18eb87bd4 Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Sat, 6 Nov 2021 23:21:14 -0400 Subject: [PATCH 16/17] Makes plot --- Exercise8Script.R | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Exercise8Script.R b/Exercise8Script.R index f617569..f1f16b9 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -21,9 +21,17 @@ UWdf<-data.frame(UWtime, UWcscore) MSUdf<-data.frame(MSUtime, MSUcscore) #Make plot -library(ggplot2) -ggplot()+geom_line(data = UWdf, aes(x=UWtime, y=UWcscore),color="red")+geom_line(data=MSUdf, aes(x=MSUtime, y=MSUcscore), color = "darkgreen")+ ggtitle("Cumulative Score for UW vs MSU Basketball Game")+ xlab("Time")+ylab("Cumulative Score") +plot(UWdf, type = "l", col = "red", main = "Cumulative Score for UW vs MSU Basketball Game", xlab = "Time", ylab = "Cumulative Score") +lines(MSUdf, type = "l", col = "green") +legend("topleft", legend=c("UW", "MSU"), col=c("red", "green"), lty = 1, lwd = 2, title = "Team", box.lty=0, cex = 0.75) + +library(ggplot2) +ggplot()+geom_line(data = UWdf, aes(x=UWtime, y=UWcscore), color = "red")+ + geom_line(data=MSUdf, aes(x=MSUtime, y=MSUcscore), color = "darkgreen")+ + ggtitle("Cumulative Score for UW vs MSU Basketball Game")+ + xlab("Time")+ylab("Cumulative Score")+legend("topleft", legend = c("UW", "MSU"), col = c("red", "darkgreen")) + ####Part 2 ###Guess my number From 6dcc08778a4150ee078ebbe7a3467006bba88ed2 Mon Sep 17 00:00:00 2001 From: Marlee Shaffer Date: Sat, 6 Nov 2021 23:21:52 -0400 Subject: [PATCH 17/17] Deletes plot code that doesnt work --- Exercise8Script.R | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Exercise8Script.R b/Exercise8Script.R index f1f16b9..b47e70f 100644 --- a/Exercise8Script.R +++ b/Exercise8Script.R @@ -25,13 +25,6 @@ plot(UWdf, type = "l", col = "red", main = "Cumulative Score for UW vs MSU Baske lines(MSUdf, type = "l", col = "green") legend("topleft", legend=c("UW", "MSU"), col=c("red", "green"), lty = 1, lwd = 2, title = "Team", box.lty=0, cex = 0.75) - -library(ggplot2) -ggplot()+geom_line(data = UWdf, aes(x=UWtime, y=UWcscore), color = "red")+ - geom_line(data=MSUdf, aes(x=MSUtime, y=MSUcscore), color = "darkgreen")+ - ggtitle("Cumulative Score for UW vs MSU Basketball Game")+ - xlab("Time")+ylab("Cumulative Score")+legend("topleft", legend = c("UW", "MSU"), col = c("red", "darkgreen")) - ####Part 2 ###Guess my number