Welcome to R!

Expressions

Let’s start with simple arithmatic expressions.

1 + 1
10 - 1
10 * 10

What you just used are arithmetic operators. (e.g., +,-,*,/)

Functions

A function is a piece of code written to carry out a specific task. R has a lot of built-in functions. Let’s try a few.

sum(1,2,3) #sum function 
mean(1,2,3) #mean function
print("My name is") #print function

Data Types

R supports a few basic data types: numeric, logical, character.

Numeric: Decimal values are called numerics in R. It is the default computational data type.

class(3.5)
class(3.5) #Use class function to determine the data type

Logical: Represented by TRUE and FALSE. There are two possible values.

Logical Operators

Use logical expressions to specify the conditions. : >: Greater than : >: Less than or equal to : ==: Equal to : !=: Not equal to

3.5 == 1
5 > 3

Character: A string of letters or values.

class("My name is Marisa. I am 29 years old")
## [1] "character"

Setting Variables You can assign a variable using (<-) or (=). When you define a variable, the variable will be held in your environment.

name = "My name is Marisa"
print(name)

t = 5
print(t)

Data Structures (Chapter 5 in R Cookbook) R supports different data structures, including vectors, factors, lists, and data frames.

The basic data struction in R is a vector.

  1. Vectors are homogenous.
numeric_vector = c(1,2,3) #numerical
numeric_vector

character_vector = c("a", "b", "c") #characters 
character_vector

logical_vector = c(T,F,T) #TRUE/FALSE
logical_vector

numeric_vector = c(1,2,"three") 
numeric_vector
class(numeric_vector)
  1. Vectors can be indexed by position.
logical_vector[1]
logical_vector[c(1,2)]
  1. Vectors can have names.
names(numeric_vector) = c("Anna", "Claude", "Christina", "MaryRose", "Tara")
numeric_vector

Practice

# Create Vector 
poker_vector = c(140, -50, 20, -120, 240)
blackjack_vector = c(-24, -50, 100, -350, 10)
poker_vector
blackjack_vector

# Assign names to the vector
names(poker_vector) = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") #Add names to the vectors
names(blackjack_vector) = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
poker_vector
# What day did I win the most money?
total_daily = poker_vector + blackjack_vector
total_daily

# How much did I win during the week?
total = sum(poker_vector, blackjack_vector)
total

# How much did I win on Wednesday playing poker?
poker_wednesday = poker_vector[3] #select a specific location in the vector 
poker_wednesday

# How much did I win on Wednesday?
poker_wednesday_win = poker_vector[3] + blackjack_vector[3]
poker_wednesday_win

# How much did I win playing poker during the middle of the week?
poker_midweek = poker_vector[c(2,3,4)] 
poker_midweek

sum(poker_midweek)

# Did I make a profit gambling?

total > 0
library(datasets)

str(mtcars)
head(mtcars, 20)

Other important R information

  1. R Packages R is an open-source package. You can use base functions with R, but you can also install packages.
  2. Help function Use the help() (also “?”) function to get information on any function or package.