-
Notifications
You must be signed in to change notification settings - Fork 19
Sydney Harris Exercise10 submission #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sydneyharris
wants to merge
2
commits into
qtran4:main
Choose a base branch
from
sydneyharris:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #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") | ||
|
|
||
| #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] | ||
| 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 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 | ||
| 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 | ||
| #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 | ||
| guess_my_number <- function() { | ||
| #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(number<guess){ | ||
| #done if number is lower than guess | ||
| print ('lower') | ||
| }else if (number==guess){ | ||
| #done if the number is equal to the guess | ||
| print ('Correct! The number was:') | ||
| print(number) | ||
| return('end of game') | ||
| }} | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be a condition when i > 10 then the console will print out that this is out of guesses. |
||
| #play game in the console by calling the function | ||
| guess_my_number() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the for loops for cumulative sum as well. Checking this code:
load table of scoring
scoring=read.table("UWvMSU_1-22-13.txt",header=TRUE,sep="\t",stringsAsFactors=FALSE)
look at data
dim(scoring)
head(scoring)
preallocating matrix to store cumulative scores
cum_scores=matrix(NA,nrow(scoring)+1,3)
cum_scores[,1]=c(0,scoring[,1])
cum_scores[1,2:3]=0
colnames(cum_scores)=c("time","UW","MSU")
looping through individual scoring events
for(i in 1:nrow(scoring)){
if(scoring[i,2]=="UW"){
cum_scores[(i+1),2]=cum_scores[i,2]+scoring[i,3]
cum_scores[(i+1),3]=cum_scores[i,3]
}else{
cum_scores[(i+1),2]=cum_scores[i,2]
cum_scores[(i+1),3]=cum_scores[i,3]+scoring[i,3]
}
}