From ebdba2fab19cb5c518569565a384f36d091cd750 Mon Sep 17 00:00:00 2001 From: Johanna1403 Date: Mon, 8 Nov 2021 22:51:50 -0500 Subject: [PATCH 1/3] Initial task 1 --- Exercise8.R | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Exercise8.R diff --git a/Exercise8.R b/Exercise8.R new file mode 100644 index 0000000..4d3082e --- /dev/null +++ b/Exercise8.R @@ -0,0 +1,29 @@ +# Task 1 + +setwd("/Users/johi/Desktop/Courses/Biocomputing/Biocomp_tutorial10") +data <- read.table(file = "UWvMSU_1-22-13.txt", header = TRUE, sep = "\t", stringsAsFactors = FALSE) +tail(data) + +sum_UW = 0 +sum_MSU = 0 + +UW = numeric() +MSU = numeric() + +for (i in 1:length(data$team)) { + if (data$team[i] == "UW") { + sum_UW = sum_UW + data$score[i] + print("UW") + print(sum_UW) + UW[i] = paste(sum_UW) + } + else { + sum_MSU = sum_MSU + data$score[i] + print("MSU") + print(sum_MSU) + MSU[i] = paste(sum_MSU) + } +} +length(MSU) + +plot(x_time, MSU, type = 'l') From bbdc81175ce9d52cd7fbc891bc7b0f13351a8ecd Mon Sep 17 00:00:00 2001 From: Johanna1403 Date: Mon, 8 Nov 2021 23:18:47 -0500 Subject: [PATCH 2/3] initial task2 --- Exercise8.R | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/Exercise8.R b/Exercise8.R index 4d3082e..20b7e6b 100644 --- a/Exercise8.R +++ b/Exercise8.R @@ -7,23 +7,45 @@ tail(data) sum_UW = 0 sum_MSU = 0 -UW = numeric() -MSU = numeric() +UW = 0 +MSU = 0 for (i in 1:length(data$team)) { if (data$team[i] == "UW") { sum_UW = sum_UW + data$score[i] - print("UW") - print(sum_UW) - UW[i] = paste(sum_UW) + UW = c(UW, sum_UW) } else { sum_MSU = sum_MSU + data$score[i] - print("MSU") - print(sum_MSU) - MSU[i] = paste(sum_MSU) + MSU = c(MSU, sum_MSU) } } -length(MSU) +x_time = 1:length(MSU) plot(x_time, MSU, type = 'l') +xt = 1:length(UW) +lines(xt, UW) + +# Task2 +# Guess my number + +rand = round(runif(1, 1, 100)) + +for (i in 1:10) { + input = strtoi(readline(prompt = "Guess the number: "), base = 0L) + if (rand < input) { + print("Try lower.") + } + else if (rand > input) { + print("Try higher.") + } + else { + print("Well done!") + break + } +} + + + + + From 605061f62ea64e62492156fe11bdc5a9f73070ad Mon Sep 17 00:00:00 2001 From: Johanna1403 Date: Thu, 11 Nov 2021 19:24:13 -0500 Subject: [PATCH 3/3] Improved task 1 and added comments to the code. --- Exercise8.R | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Exercise8.R b/Exercise8.R index 20b7e6b..e9f5fdf 100644 --- a/Exercise8.R +++ b/Exercise8.R @@ -1,36 +1,49 @@ # Task 1 +# set the working directory and read the data setwd("/Users/johi/Desktop/Courses/Biocomputing/Biocomp_tutorial10") data <- read.table(file = "UWvMSU_1-22-13.txt", header = TRUE, sep = "\t", stringsAsFactors = FALSE) -tail(data) +# initialize the score cumulative sum variable sum_UW = 0 sum_MSU = 0 +# initialize the empty vectors for the score cumulative sums from each timepoint UW = 0 MSU = 0 +# separate the UW and MSU data (for time purposes) +UW_data = data[data$team == "UW", ] +MSU_data = data[data$team == "MSU", ] + +# for loop with if-else statement to search through the whole data table +# cumulative sum of the score for each team is calculated and assigned to a growing vector in each loop +# a time vector for each team is created from the corresponding extracted team data time column where each time point corresponds to the cumulative sum of the score at this point for (i in 1:length(data$team)) { if (data$team[i] == "UW") { sum_UW = sum_UW + data$score[i] UW = c(UW, sum_UW) + UW_time = c(0, UW_data$time) } else { sum_MSU = sum_MSU + data$score[i] MSU = c(MSU, sum_MSU) + MSU_time = c(0, MSU_data$time) } } -x_time = 1:length(MSU) -plot(x_time, MSU, type = 'l') -xt = 1:length(UW) -lines(xt, UW) +# plotting the corresponding graphs +plot(MSU_time, MSU, type = "l", main = "UW vs. MSU basketball", xlab = "Time", ylab = "Score", col = "blue") +lines(UW_time, UW, col = "red") +legend("topleft", c("MSU", "UW"), fill = c("blue", "red")) # Task2 # Guess my number +# generating a random number rand = round(runif(1, 1, 100)) +# for loop to let the user make 10 guesses and give feedback every time using if-else statement for (i in 1:10) { input = strtoi(readline(prompt = "Guess the number: "), base = 0L) if (rand < input) {