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:

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(dplyr)
  1. From set = {1.5, 15, 0, 54, 23}, pick two numbers and perform the Following Operations:

.Subtraction .Multiplication .square root

5 * 54
## [1] 270
sqrt(54)
## [1] 7.348469
23 - 15
## [1] 8

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)))
## [1] 73
#Find the Maximum and Minimum score
min(scores)
## [1] 0
max(scores)
## [1] 100
#Find how many scores were above the average, use coding for this

sum(scores)
## [1] 659.5
abv_scores <- scores[] > mean(scores)
abv_scores
## [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE
#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]
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_1 <- c(1, 2, 3, 4, 5)
print(vec_1)
## [1] 1 2 3 4 5
# Create a vector with a sequence of numbers from 1 to 10 with a step of 2. Print it

seq_num <- seq(from = 1, to = 10, by = 2)

print(seq_num)
## [1] 1 3 5 7 9
# Create a vector by repeating a sequence from 1 to 3, 2 times. Print it.

seq_num2 <- seq(from = 1, to = 3, by = 2)
print(seq_num2)
## [1] 1 3

Problem 4

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

# Check the class of the vector

class(mixed_vector)
## [1] "character"
print(mixed_vector)
## [1] "1"    "two"  "3"    "TRUE" "2"
## Coerce to numeric and print it 


mixed_vector_num <- as.numeric(mixed_vector)
## Warning: NAs introduced by coercion
mixed_vector_num
## [1]  1 NA  3 NA  2
class(mixed_vector_num)
## [1] "numeric"