From 193143a79fc537244cd359b0b9cc5184e9459e30 Mon Sep 17 00:00:00 2001 From: Matan Horovitz Date: Mon, 6 Mar 2023 21:36:23 +0200 Subject: [PATCH] Why not? --- amazing.R | 34 ++++++++++++++++++++++++++++++++++ mean.R | 23 +++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 amazing.R create mode 100644 mean.R diff --git a/amazing.R b/amazing.R new file mode 100644 index 0000000..3aff1fe --- /dev/null +++ b/amazing.R @@ -0,0 +1,34 @@ +#Create vector from 1 to 10 +num <- c(seq(1:10)) +#Create empty vector named 'amazing' +amazing <-c() +#Iterate over all numbers from 1 to the length of the vector (10) +for (i in 1:length(num)) +{ + #If the number is greater or equal to 1 AND ALSO (&) smaller or equal to 3... + if(i >= 1 & i <=3) + # ^ this bit is important + { + cat(i, "is low\n") + #Add "low" to list called 'Amazing' + amazing[i] <- "low" + #Once this condition is satisfied, move on to next item in loop (if on 1, move on to 2, etc) + next + } + #If the number is greater or equal to 3 AND ALSO (&) smaller or equal to 6... + else if(i > 3 & i <=6) + # ^ this is like two IF's + { + cat(i, "is medium\n") + amazing[i] <- "medium" + next + } + + else if(i > 6 & i <=10) + # ^ this bit is also important + cat(i, "is high\n") + amazing[i] <- "high" + next +} +amazing +sleep["Amazing"] <- amazing \ No newline at end of file diff --git a/mean.R b/mean.R new file mode 100644 index 0000000..9ffa2a4 --- /dev/null +++ b/mean.R @@ -0,0 +1,23 @@ +#Create two vectors with random, normal distribution numbers: one with even, and another with odd number of elements. +bunch_of_nums_even <- rnorm(100) +bunch_of_nums_odd <- rnorm(101) +#Sort one of the vectors according to number size +sorted_nums <- sort(bunch_of_nums_even) #< I used even for this example - switch to odd to test that as well. +num_length <- length(sorted_nums) +#If the length of the sorted vector is divisble by 2... +if ((num_length %% 2) == 0) { + print("Vector is even") + #... create a vector of the 2 middle elements... + num_medians <- c((num_length/2),((num_length/2)+1)) + #... and calculate their average; that is the mean. + num_median <- mean(sorted_nums[num_medians]) + # ^ This is a vector of the 2 middle spots in our even vector +} else #< If the length of the sorted vector is odd... +{ + print("Vector is odd") + #Get the index of the median number by substracting 1 from the total count, divide by half and add one again. + num_median_index <- (((num_length + 1)/2)) #