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
54 - 23
## [1] 31
1.5*15
## [1] 22.5
sqrt(54)
## [1] 7.348469
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
av_scores <- mean(scores)
round(mean(av_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.27]
## [1] 87.0 100.0 91.0 95.0 81.5 74.0 92.0
#create a new set of scores, call it new_scores. The new list should include elements 2 to 6
scores[scores<73.27]
## [1] 0 39
# Create a vector with the number 1 repeated 5 times. Print it
vector_name <- c(1, 1, 1, 1, 1)
print(vector_name)
## [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
vector_name1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(vector_name1)
## [1] 1 2 3 4 5 6 7 8 9 10
# Create a vector by repeating a sequence from 1 to 3, 2 times. call it
vector_name2 <- c(1, 2, 3, 1, 2, 3)
print(vector_name2)
## [1] 1 2 3 1 2 3
# Create a vector with different class types
mixed_vector <- c(1, "two", 3.0, TRUE, 2)
# Check the class of the vector
summary(mixed_vector)
## Length Class Mode
## 5 character character
## Coerce to numeric and print it
numeric_vector <- as.numeric(mixed_vector)
## Warning: NAs introduced by coercion
print(numeric_vector)
## [1] 1 NA 3 NA 2