Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions exercise10script.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
setwd("/Users/7907blueyes/Desktop/Biocomputing/R/Exercise10")

# ggplot2 and load
library(ggplot2)

###### PROBLEM 1
# read in data file
scores <- read.table("UWvMSU_1-22-13.txt", header = TRUE, sep = "\t", stringsAsFactors = FALSE)

# create empty score for each time
UW <- numeric(nrow(scores))
MSU <- numeric(nrow(scores))

# create a new data frame
cumulative.score <- data.frame(Time = scores$time, UW, MSU)

# calculate cumulative scores
# separate scores by team
for(i in 1:nrow(scores)){
if(scores$team[i] == "UW"){
cumulative.score$UW[i] <- scores$score[i]
}else if(scores$team[i] == "MSU"){
cumulative.score$MSU[i] <- scores$score[i]
}
}
# make scores cumulative
for(i in 2:nrow(cumulative.score)){
cumulative.score$UW[i] <- cumulative.score$UW[i] + cumulative.score$UW[i -1]
cumulative.score$MSU[i] <- cumulative.score$MSU[i] + cumulative.score$MSU[i -1]
}

# make a graph
ggplot(data = cumulative.score, aes(x = Time)) +
geom_line(aes(y = UW), color = "black") +
geom_line(aes(y = MSU), color = "blue") +
ylab("Score")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternative way to do this:

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]
}
}

##########
########## PROBLEM 2
##########

# get user number, random number and max number of guesses
guess <- scan(n=1)
randNum <- sample(1:100, 1)
maxGuesses <- 10

# for loop to see if correct
for(try in 1:maxGuesses){
if(guess == randNum){
print("Correct")

# break for loop if guess is made before the tenth try
if (try < 10) break

}else if(try == maxGuesses){
print("Game Over :(")
}else if(guess < randNum){
print("Higher")
guess <- scan(n=1)
}else {
print("Lower")
guess <- scan(n=1)
}
}