R

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

# I picked 15 and 54
15 - 54
## [1] -39
# multiply them
15 * 54
## [1] 810
# square root
sqrt(15)
## [1] 3.872983
sqrt(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)

# average score
average <- round(mean(scores), 2)
average
## [1] 73.28
# highest and lowest score
max(scores)
## [1] 100
min(scores)
## [1] 0
# scores above average
sum(scores > average)
## [1] 7
# scores from index 2 to 6
new_scores <- scores[2:6]
new_scores
## [1] 100.0  91.0  95.0  81.5   0.0

Problem 3:

Read section 2.3, then answer the following questions

# repeat 1 five times
x <- rep(1, 5)
x
## [1] 1 1 1 1 1
# numbers from 1 to 10 by 2
x <- seq(1, 10, by = 2)
x
## [1] 1 3 5 7 9
# repeat 1 to 3 two times
x <- rep(1:3, 2)
x
## [1] 1 2 3 1 2 3

Problem 4

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

# check the class
class(mixed_vector)
## [1] "character"
# change it to numbers
as.numeric(mixed_vector)
## Warning: NAs introduced by coercion
## [1]  1 NA  3 NA  2