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 #
.Subtraction .Multiplication .square root
23 - 15
## [1] 8
1.5 * 15
## [1] 22.5
sqrt(54)
## [1] 7.348469
sqrt(54-23)
## [1] 5.567764
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
### mean(scores) to find the average of the scores.
### round(mean(scores),2) to round the average of the scores to two decimals.
#Find the Maximum and Minimum score
max(scores)
## [1] 100
min(scores)
## [1] 0
### max(scores) to find the maximum score.
### min(scores) to find the minimum score.
#Find how many scores were above the average, use coding for this
scores [scores > mean(scores)]
## [1] 87.0 100.0 91.0 95.0 81.5 74.0 92.0
### scores [scores > mean(scores)] to find which scores were above average.
#Create a new set called new_scores that includes the elements from indices 2 to 6 of the scores vector
new_scores <- c(100,91,95,81.5,0)
### new_scores <- c(100,91,95,81.5,0) to create a new vector called new_scores with numbers from old one excluding 87,39,74, and 92.
Read section 2.3, then answer the following questions
# Create a vector with the number 1 repeated 5 times. Print it
one_five_times <- c(1,1,1,1,1)
one_five_times
## [1] 1 1 1 1 1
### one_five_times <- c(1,1,1,1,1) to create a vector with the number 1 repeated 5 times.
# Create a vector with a sequence of numbers from 1 to 10 with a step of 2. Print it
one_to_ten_counting_in_twos <- c(2,4,6,8,10)
one_to_ten_counting_in_twos
## [1] 2 4 6 8 10
### one_to_ten_counting_in_twos <- c(2,4,6,8,10) to create a vector counting to ten in twos.
# Create a vector by repeating a sequence from 1 to 3, 2 times. Print it.
one_two_three_three_times <- c(1,2,3,1,2,3,1,2,3)
one_two_three_three_times
## [1] 1 2 3 1 2 3 1 2 3
### one_two_three_three_times <- c(1,2,3,1,2,3,1,2,3) to create a vector that repeats 1 to 3 3 times.
mixed_vector <- c(1, "two", 3.0, TRUE, 2)
# Check the class of the vector
class(mixed_vector)
## [1] "character"
## class(mixed_vector) to check class of vector.
## Coerce to numeric and print it
as.numeric(mixed_vector)
## Warning: NAs introduced by coercion
## [1] 1 NA 3 NA 2
### as.numeric(mixed_vector) to coerce to numeric.