forked from timaburton/Biocomp_tutorial10
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise8.R
More file actions
47 lines (39 loc) · 1.39 KB
/
Copy pathExercise8.R
File metadata and controls
47 lines (39 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#### Exercise 8 - Using control structures in R
### Problem 1 - generate a plot using score-by-score information from "UWvMSU_1-22-13.txt"
## Load data file
scoreInfo<-read.delim("UWvMSU_1-22-13.txt")
## Separate score-by-score data by school
UWscores<-scoreInfo[scoreInfo$team=='UW',]
MSUscores<-scoreInfo[scoreInfo$team=='MSU',]
## Generate a matrix or dataframe with cumulative score for each team whenever either team scores
# Write for loops to sum cumulative scores for each team when they score
cumScoreUW<-numeric(length(UWscores$score))
for(i in 1:length(UWscores$score)){
cumScoreUW[i]<-sum(UWscores$score[1:i])
}
cumScoreMSU<-numeric(length(MSUscores$score))
for(i in 1:length(MSUscores$score)){
cumScoreMSU[i]<-sum(MSUscores$score[1:i])
}
## Plot cumulative score for each team as a function of time
x1<-UWscores$time
x2<-MSUscores$time
y1<-cumScoreUW
y2<-cumScoreMSU
plot(x1,y1,type='l',xlab='time',ylab='points scored')
lines(x2,y2)
### Problem 2 - "guess my number game"
## Generate random number between 1 and 100
randomnumber<-sample(1:100,1)
## Allow 10 guesses
# Write a for loop to loop through guesses with the outputs 'higher', 'lower', or 'correct'.
for (i in 1:10) {
userGuess<-readline(prompt="Enter guess between 1 and 100: ")
if (userGuess < randomnumber){
print("Higher...")
} else if (userGuess > randomnumber){
print("Lower...")
} else {
print ("Correct!!")
}
}