Why not?
This commit is contained in:
34
amazing.R
Normal file
34
amazing.R
Normal file
@@ -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
|
||||
23
mean.R
Normal file
23
mean.R
Normal file
@@ -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)) #<There's probably a better way to do this.
|
||||
#The median is the number in the index we figured out earlier; pull it from the sorted vector.
|
||||
num_median <- sorted_nums[num_median_index]
|
||||
}
|
||||
cat("Median is:", num_median)
|
||||
Reference in New Issue
Block a user