INTRODUCTION TO R

NOTES ON DATA CAMPS INTRODUCTION TO R
2

R

EASY

last hacked on Jul 16, 2018

Notes on [introduction to R course on DataCamp](https://www.datacamp.com/courses/free-introduction-to-r)

Arithmetic with R

5 + 5

# A subtraction
5 - 5

# A multiplication
5 * 5

# A division
(5 + 5) / 2

# Exponentiation
2^5

# Modulo
28 %% 6

Variable assignment

# Assign the value 42 to x
x <- 93

# Print out the value of the variable x
93

Variable Assignment 2

# Add value of 3 to my_cats
my_cats <- 3

# Print out the value of my_cats
my_cats

Variable Assignment 3

# Add value of 3 to my_cats and value of 2 to my_dogs
my_cats <- 3
my_dogs <- 2

# Create new variable of my_pets
my_pets <- my_cats + my_dogs

# Print out my_pets
my_pets

Types of Data in R

Integers, numerics, logical, and characters are four basic types of data when being introduced to R.

• Integers are whole numbers and numerics are decimal numbers. Note that integers are also identified as numerics.

• Logical values are (TRUE) or (FALSE).

• Characters are "text" or sting.

# Variable ex_numeric to 2.5
ex_numeric <- 2.5

# Variable ex_integer to 2
ex_integer <- 2

# Variable ex_logical to TRUE
ex_logical <- TRUE

# Variable ex_text to "fun"
ex_text <- "fun"

Vectors

# Define my_name variable with value of "Jane"
my_name <- "Jane"

# Create vector of logical values
logical_vector <- c(TRUE, TRUE, FALSE, TRUE, FALSE)

# Create vector of numerical values
race_winners <- c(3, 7, 8, 11, 15, 21, 32, 55, 60)

# Create vector of character values
types_wine <- c("Rose", "Merlot", "Chardonnay", "Pinot noir")

# Create two separate vectors
vector_one <- c(3, 7, 19, 22)
vector_two <- c(4, 11, 32, 23)

# Create new vector vector_total with sum of vectors
vector_total <- vector_one + vector_two

Selecting Elements from Vectors

# Select element 3 from vector_one name it third_vc
third_vc <- vector_one

# Select elements 1 3 and 4 from vector_two name it el_two
el_two <- vector_two[c(1, 3, 4)]

# Select elements from 1 to 3 from vector_one name is main_two
main_two <- vector_one[1:3]