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
## [1] 13.5
15*1.5
## [1] 22.5
sqrt(x=15)
## [1] 3.872983
sqrt(x=1.5)
## [1] 1.224745

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)
## [1] 73.27778
round(mean(scores),2)
## [1] 73.28
#Find the Maximum and Minimum score
max(scores)
## [1] 100
min(scores)
## [1] 0
#Find how many scores were above the average, use coding for this
scores[scores>73.27778]
## [1]  87.0 100.0  91.0  95.0  81.5  74.0  92.0
#Create a new set called new_scores that includes the elements from indices 2 to 6 of the scores vector

Problem 3:

Read section 2.3, then answer the following questions

# Create a vector with the number 1 repeated 5 times. Print it
my_vec <- c(1,1,1,1,1)
my_vec
## [1] 1 1 1 1 1
print(my_vec)
## [1] 1 1 1 1 1
# Create a vector with a sequ5ence of numbers from 1 to 10 with a step of 2. Print it
my_vec2 <- c(1,2,3,4,5,6,7,8,9,10)
my_vec2
##  [1]  1  2  3  4  5  6  7  8  9 10
my_seq <- 1:10
my_seq <- seq(from = 1, to = 10, by = 2)
my_seq
## [1] 1 3 5 7 9
print(my_seq)
## [1] 1 3 5 7 9

Problem 4

mixed_vector <- c(1, "two", 3.0, TRUE, 2)
class(mixed_vector)
## [1] "character"
# Check the class of the vector
-
## Coerce to numeric and print it 
as.numeric(mixed_vector)
## Warning: NAs introduced by coercion
## [1] -1 NA -3 NA -2
print(mixed_vector)
## [1] "1"    "two"  "3"    "TRUE" "2"