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
52 changes: 52 additions & 0 deletions EX10.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Exercise 10

# Generate a line graph depicting the cumulative score for each team as a
# function of time in the game

setwd("/Users/bethoceguera/Documents/PhD Year 1/First Semester/Introduction to Biocomputing/Exercises/Exercise10")
data <- read.table("UWvMSU_1-22-13.txt", head=TRUE,sep = "\t")
library(ggplot2)

#Define the working variable
uwScores <- data[data$team == "UW",]
msuScores <- data[data$team == "MSU",]

#Table acummulating um scores
for(i in 2:nrow(uwScores)){
uwScores[i,3] <- uwScores[i,3]+uwScores[(i-1),3]
}

#Table acummulating msu scores
for(i in 2:nrow(msuScores)){
msuScores[i,3] <- msuScores[i,3]+msuScores[(i-1),3]
}

#Plot with two lines for um and msu
ggplot() +
geom_line(data=uwScores,aes(x=time,y=score), color = "darkred") +
geom_line(data=msuScores,aes(x=time,y=score), color="steelblue", linetype="twodash")

@qtran4 qtran4 Dec 3, 2022

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.

an alternative way:

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

##DONE

#2. Write a game called “guess my number”.
# The computer will generate a random number between 1 and 100.
# The user types in a number and the computer replies “lower” if the random number is lower than the guess, “higher” if the random number is higher, and “correct!” if the guess is correct.
# The player can continue guessing up to 10 times.

maxGuesses=10
randomNumber <- sample(1:100, 1)

for(i in 1:10) {
guess=readline("Guess a random number 1 through 100: ")
if (i==maxGuesses){
print("Out of Guesses")
break
} else if (guess == randomNumber){
print("Correct!")
break
}else if (guess > randomNumber){
print("Lower")
}else if (guess < randomNumber){
print("Higher")
}
} ##DONE