-
Notifications
You must be signed in to change notification settings - Fork 19
Answers to Exercise 10 #11
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
reyort3
wants to merge
2
commits into
qtran4:main
Choose a base branch
from
reyort3: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,60 @@ | ||
| #Answers to Exercise 10 | ||
|
|
||
| #Answers to Prompt one | ||
|
|
||
| #set to correct directory and check for packages | ||
| getwd() | ||
| setwd("C:/Users/ortre/Exercise10") | ||
| library(ggplot2) | ||
|
|
||
| #retrieving data | ||
| scores_data<-read.table("UWvMSU_1-22-13.txt", header = TRUE) | ||
| #creating a blank score slate for UW and MSU | ||
| MSU<-numeric(nrow(scores_data)) | ||
| UW<-numeric(nrow(scores_data)) | ||
| #creating data frame | ||
| scores<-data.frame(time=scores_data$time, UW, MSU) | ||
| #Now to make a for loop to add score as time progresses | ||
| for (i in 1:nrow(scores_data)){ | ||
| if(scores_data$team[i]=="MSU"){ | ||
| #this is for team MSU, if they score | ||
| scores$MSU[i]<-scores_data$score[i] | ||
| }else{ | ||
| #only one other team, so else is for UW | ||
| scores$UW[i]<-scores_data$score[i] | ||
| } | ||
| } | ||
| ##getting the cumulative scores for UW and MSU | ||
| for (i in 2:nrow(scores_data)){ | ||
| scores$MSU[i]<-scores$MSU[i]+scores$MSU[i-1] | ||
| } | ||
| for (i in 2:nrow(scores_data)){ | ||
| scores$UW[i]<-scores$UW[i]+scores$UW[i-1] | ||
| } | ||
| #creating the plot | ||
| ggplot(data=scores, aes(x=time)) + | ||
| geom_line(aes(y=MSU), color ="black") + | ||
| geom_line(aes(y=UW), color="blue") + | ||
| xlab("time") + | ||
| ylab("Team Score") | ||
|
|
||
| #answer to prompt 2 | ||
|
|
||
| #creating a random number picker | ||
| RandomNumb<-sample(x=c(1:100), size=1) | ||
| #For loop for guessing game, max number of tries is 10 | ||
|
|
||
| for (i in 1:11){ | ||
| answer=readline(prompt = "Guess a number from 1-1oo") | ||
| if (answer<RandomNumber){ | ||
| print("HIGHER") | ||
| }else if (answer>RandomNumber){ | ||
| print("LOWER") | ||
| }else if (answer==RandomNumber){ | ||
| print("CORRECT!") | ||
| break #a break to end game if guess is correct and before 11th try | ||
| }elseif (i==11){ | ||
| print("YoU LOSE") | ||
| break # out of guesses | ||
| } | ||
| } | ||
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 combine the 2 for loop:
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]
}
}