From 047835e8c66f83b47b4f60d0c624ad29cac88210 Mon Sep 17 00:00:00 2001 From: sydneyharris <114443941+sydneyharris@users.noreply.github.com> Date: Sun, 20 Nov 2022 11:58:37 -0500 Subject: [PATCH 1/2] Add files via upload --- Exercise10.R | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Exercise10.R diff --git a/Exercise10.R b/Exercise10.R new file mode 100644 index 0000000..8c730e0 --- /dev/null +++ b/Exercise10.R @@ -0,0 +1,91 @@ +setwd("/users/sydneyharris/Desktop/") + +#Question 1 +UW_MSU <- read.table("/users/sydneyharris/desktop/UWvMSU_1-22-13.txt", header = T, sep = "\t") + +#set up vectors to hold information +UW_Total = numeric(length(c(1:nrow(UW_MSU)))) +MSU_Total = numeric(length(c(1:nrow(UW_MSU)))) +UW_scores = numeric(length(c(1:nrow(UW_MSU)))) +MSU_scores = numeric(length(c(1:nrow(UW_MSU)))) +#calculate cumulative score for UW and add column in UW_MSU called UW_Total +#for each row in the data set, if the team is UW, it will store the points gained (from the score) in the vector UW_scores +#if the team is not UW (by default of this data set, it has to be MSU), it will store a 0 in the vector UW_scores because UW gained 0 points +for (i in 1:nrow(UW_MSU)){ + if (UW_MSU$team[i] == "UW"){ + UW_scores[i] <- UW_MSU$score[i] + }else { + UW_scores[i] <- 0 + }} +#creates a column in UW_MSU called UW_Total with the cumulative sum of the UW team when a basket is scored regardless of team +UW_MSU$UW_Total <- c(cumsum(UW_scores)) + +#calculate cumulative score for MSU and add column in UW_MSU called MSU_Total +#for each row in the data set, if the team is MSU, it will store the score in the vector MSU_scores +#if the team is not MSU (by default of this data set, it has to be UW), it will store a 0 in the vector MSU_scores because MSU gained 0 points +for (i in 1:nrow(UW_MSU)){ + if (UW_MSU$team[i] == "MSU"){ + MSU_scores[i] <- UW_MSU$score[i] + }else { + MSU_scores[i] <- 0 + }} +#creates a column in UW_MSU called MSU_Total with the cumulative sum of the MSU team when a basket is scored regardless of team +UW_MSU$MSU_Total <- c(cumsum(MSU_scores)) + +#graph of each team's cumulative score over time +ggplot(UW_MSU, aes(x=time))+ + geom_line(aes(y = UW_Total, color = "UW")) + + geom_line(aes(y = MSU_Total, color = "MSU"))+ + xlab("Time (mins)")+ + ylab("Score") + +#Question 2 +#generate random number and save that as variable x +x<-sample(x=c(1:100), size =1) +#Use a for loop for each of the 10 guesses +#use if else statements to give feedback on the guess +#use readline(prompt="Guess:") to allow the user to make multiple interactive guesses + +#this version doesn't generate new random number each time game is played +guess_my_number <- function() { + answer <- readline(prompt= "I'm thinking of a random number. Can you guess it? Type Y or N?") + if (answer=="Y"){ + x<- sample(x=c(1:100), size =1) + } else { + return("end") + } + + for (i in 1:10) { + y <- readline(prompt = 'Guess:') + if (x>y){ + #done number is higher than guess + print ('higher') +}else if(xy){ + #done number is higher than guess + print ('higher') + }else if(x Date: Mon, 21 Nov 2022 15:19:19 -0500 Subject: [PATCH 2/2] Add files via upload --- Exercise10.R | 92 +++++++++++++++++++--------------------------------- 1 file changed, 33 insertions(+), 59 deletions(-) diff --git a/Exercise10.R b/Exercise10.R index 8c730e0..f05ae2a 100644 --- a/Exercise10.R +++ b/Exercise10.R @@ -1,35 +1,31 @@ +#set working directory and load ggplot setwd("/users/sydneyharris/Desktop/") +library(ggplot2) #Question 1 +#read data into R and save as variable UW_MSU <- read.table("/users/sydneyharris/desktop/UWvMSU_1-22-13.txt", header = T, sep = "\t") -#set up vectors to hold information -UW_Total = numeric(length(c(1:nrow(UW_MSU)))) -MSU_Total = numeric(length(c(1:nrow(UW_MSU)))) -UW_scores = numeric(length(c(1:nrow(UW_MSU)))) -MSU_scores = numeric(length(c(1:nrow(UW_MSU)))) -#calculate cumulative score for UW and add column in UW_MSU called UW_Total -#for each row in the data set, if the team is UW, it will store the points gained (from the score) in the vector UW_scores -#if the team is not UW (by default of this data set, it has to be MSU), it will store a 0 in the vector UW_scores because UW gained 0 points +#pre-allocate vectors to hold information +UW_scores = numeric(length(c(1:nrow(UW_MSU)))) #points gained by UW every time a basket is scored regardless of team +MSU_scores = numeric(length(c(1:nrow(UW_MSU)))) #points gained by MSU every time a basket is scores regardless of team + +#for loop with if-else statements for (i in 1:nrow(UW_MSU)){ if (UW_MSU$team[i] == "UW"){ +#for each row in the data set, if the team is UW, it will store the points gained in the vector UW_scores +#it will store a 0 in the vector MSU_scores because MSU gained 0 points UW_scores[i] <- UW_MSU$score[i] - }else { + MSU_scores[i] <- 0 + }else if (UW_MSU$team[i] == "MSU"){ +#for each row in the data set, if the team is MSU, it will store the points gained in the vector MSU_scores +#it will store a 0 in the vector UW_scores because UW gained 0 points + MSU_scores[i] <- UW_MSU$score[i] UW_scores[i] <- 0 }} -#creates a column in UW_MSU called UW_Total with the cumulative sum of the UW team when a basket is scored regardless of team -UW_MSU$UW_Total <- c(cumsum(UW_scores)) -#calculate cumulative score for MSU and add column in UW_MSU called MSU_Total -#for each row in the data set, if the team is MSU, it will store the score in the vector MSU_scores -#if the team is not MSU (by default of this data set, it has to be UW), it will store a 0 in the vector MSU_scores because MSU gained 0 points -for (i in 1:nrow(UW_MSU)){ - if (UW_MSU$team[i] == "MSU"){ - MSU_scores[i] <- UW_MSU$score[i] - }else { - MSU_scores[i] <- 0 - }} -#creates a column in UW_MSU called MSU_Total with the cumulative sum of the MSU team when a basket is scored regardless of team +#creates 2 columns in UW_MSU called UW_Total and MSU_Total with the cumulative sum of each team when a basket is scored regardless of team +UW_MSU$UW_Total <- c(cumsum(UW_scores)) UW_MSU$MSU_Total <- c(cumsum(MSU_scores)) #graph of each team's cumulative score over time @@ -40,52 +36,30 @@ ggplot(UW_MSU, aes(x=time))+ ylab("Score") #Question 2 -#generate random number and save that as variable x -x<-sample(x=c(1:100), size =1) +#create a vector with integers 1 to 100 +x<- c(1:100) #Use a for loop for each of the 10 guesses #use if else statements to give feedback on the guess #use readline(prompt="Guess:") to allow the user to make multiple interactive guesses - -#this version doesn't generate new random number each time game is played -guess_my_number <- function() { - answer <- readline(prompt= "I'm thinking of a random number. Can you guess it? Type Y or N?") - if (answer=="Y"){ - x<- sample(x=c(1:100), size =1) - } else { - return("end") - } + - for (i in 1:10) { - y <- readline(prompt = 'Guess:') - if (x>y){ - #done number is higher than guess - print ('higher') -}else if(xy){ + #use sample() to get a random integer from the vector x and save that as the number to be guessed + number<- sample(x=x, size =1) + print("I'm thinking of a number from 1 to 100...") + for (i in 1:10) { # using (i in 1:10) restricts the user to only 10 guesses + guess <- readline(prompt = 'Guess:') #prompts the reader to make a guess + #the following if-else statements call for a guess and give feedback based on the guess + if (number>guess){ #done number is higher than guess print ('higher') - }else if(x