R Basics

You can insert an R code chunk into an R Markdown document by using a keyboard shortcut. The default keyboard shortcut to insert an R code is:

Windows/Linux: Ctrl + Alt + I Mac: Command + Option + I

Make sure to comment on your code using #

Problem 1:

  1. From set = {1.5, 15, 0, 54, 23}, pick two numbers and perform the Following Operations:

.Subtraction .Multiplication .square root

15 * 23 # Performing multiplication by multiplying 15 by 23.
## [1] 345
15 - 1.5 # Performing subtraction by subtracting 1.5 from 15.
## [1] 13.5
sqrt(54) # Perfoming square root of 54.
## [1] 7.348469

Problem 2

Suppose you have a set of numbers representing the assignments scores:

scores <- c(87,100, 91, 95, 81.5, 0, 39, 74, 92)

#Calculate the average of the scores, and round your answer to two decimal number
round(mean(scores), 2) # Using the mean function to gather the average of the vector "scores", then using the round function to round the mean to two decimal values. 
## [1] 73.28
#Find the Maximum and Minimum score
max(scores) # Gathering the maximmum value within the scores vector. 
## [1] 100
min(scores) # Gathers the minimum value within the scores vector. 
## [1] 0
#Find how many scores were above the average, use coding for this
scores[scores > mean(scores)] # Prints out all the scores that are greater than the average within the scores vector. 
## [1]  87.0 100.0  91.0  95.0  81.5  74.0  92.0
sum(scores > mean(scores)) # Prints out the total amount of numbers that are greater than the average in the scores value. 
## [1] 7
#Create a new set called new_scores that includes the elements from indices 2 to 6 of the scores vector
new_scores <- scores[2 : 6] # Creates a new set of scores by using the 2nd through 6th elements of the scores vector and stores it inside new_scores. 
new_scores # Prints new_scores. 
## [1] 100.0  91.0  95.0  81.5   0.0

Problem 3:

Read section 2.3, then answer the following questions

# Create a vector with the number 1 repeated 5 times. Print it
testVector <- c(1, 1, 1, 1, 1) # A vector containing the number 1 five times. 
testVector # Prints testVector
## [1] 1 1 1 1 1
altTestVector <- rep(1, times = 5) # An alternative way of also completing this question of the problem. 
altTestVector # Prints altTestVector
## [1] 1 1 1 1 1
# Create a vector with a sequence of numbers from 1 to 10 with a step of 2. Print it
secondTestVector <- seq(from = 1, to = 10, by = 2) # secondTestVector created using the sequence function storing numbers form 1 to 10, but each number is increased by 2 instead of by 1. 
secondTestVector # Prints secondTestVector.
## [1] 1 3 5 7 9
# Create a vector by repeating a sequence from 1 to 3, 2 times. Print it.
thirdTestVector <- rep(1 : 3, times = 2) # thirdTestVector created using the repeating sequence function, repeating the numbers from 1 to 3 a total of two times. 
thirdTestVector # Prints thirdTestVector
## [1] 1 2 3 1 2 3

Problem 4

mixed_vector <- c(1, "two", 3.0, TRUE, 2)

# Check the class of the vector
class(mixed_vector) # Uses class function to identify the class structure of mixed_vector.
## [1] "character"
## Coerce to numeric and print it 
as.numeric(mixed_vector) # Uses "as.numeric()" function to coerce mixed_vector into numeric values and prints in the console log (Only prints out numeric values, every other value not numeric turns into NA). 
## Warning: NAs introduced by coercion
## [1]  1 NA  3 NA  2