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-1.5   #subtraction
## [1] 13.5
54*23    #multiplication
## [1] 1242
sqrt(54) #square root
## [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

mean(scores)             #average of scores vector
## [1] 73.27778
round(mean(scores),2)    #rounding mean of scores to 2 decimal places
## [1] 73.28
#Find the Maximum and Minimum score

max(scores)    # finding the maximum of the vector scores
## [1] 100
min(scores)    #finding the minimum of the vector scores
## [1] 0
#Find how many scores were above the average, use coding for this

mean (scores)    #finding mean of scores
## [1] 73.27778
above_average <- scores [scores > mean(scores)]  #elements greater than the mean
sum(scores > mean(scores)) 
## [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]  # create new_scores
new_scores                 #wanted to list the elements in 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


vec_one_fivetimes <- rep(1, times = 5) # define create vec_one_fivetimes
                                       # repeating the number 1 for total of 5 times
vec_one_fivetimes                      #printing the vector
## [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

my_seq <- seq( from = 1, to = 10, by = 2) #sequence starting from 1 to 10 in steps of 2, stopping at 9 because 9+2 > 10 
my_seq     #printing
## [1] 1 3 5 7 9
# Create a vector by repeating a sequence from 1 to 3, 2 times. Print it.

my_seq2 <- rep( 1:3, times = 2 ) #repeating sequence 1 to 3 , 2 times
my_seq2                          #printing
## [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)  # call the function class to find class of mixed_vector
## [1] "character"
                     # the class is character for each element 
                     # because there is the character "two"

## Coerce to numeric and print it 
numeric_vector <-as.numeric(mixed_vector)  #used numeric coercion                                                   
## Warning: NAs introduced by coercion
                                           #function to change class to numeric
# character_of_numbers <- "3.0"
# class(character_of_numbers)
# blah <-as.numeric(character_of_numbers)
# blah

numeric_vector              #printing
## [1]  1 NA  3 NA  2
                          #1, 3.0, and 2 are coerced to characters "1"                             
                          #"3.0" , and "2" and then can be coerced to                              
                          #numeric since only number in string
                          #"two" character to numeric fails so  NA
                            #TRUE coerced to character "TRUE" non number                           
                          #in character and so numeric fails so  NA