What is R and RStudio

How to install R and RStudio

Infographics

Infographics

Getting Started Working with R

Getting Help

  • If you need help on or want to access the documentation of a particular function or package, you can simply type in the console a question mark ? followed by the name of the function or package, or you can use the help() function.
?mean
help(mean)
  • To obtain a summary of an object’s structure, use the str() function.
str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
  • You can find out an object’s class with the class() function.
class(iris)
## [1] "data.frame"

Using Packages

  • To download and install a package from CRAN.
install.packages("dplyr")
  • The function library() allows you to load a package into your current R session, which will make all its functions available to use.
library(dplyr)
  • By typping consecutive colons :: after the name of a particular function, you can specify a particular function that you want to use from this package.
dplyr::select()
  • For loading a built-in dataset into the environment.
data("iris")

Working Directory

  • The getwd() function will retrieve the current working directory for your current R session, which is where inputs are found and outputs are sent.
getwd()
## [1] "C:/Users/javyr/Desktop"
  • If you want to change your current working directory, this can be accomplished with the help of the setwd() function. Inside this function you will specify the new path that you want R to associate as the current working directory.
setwd("C:/Users/javyr/")
getwd()
## [1] "C:/Users/javyr"

Vectors

  • There are multiple ways to create a vector in R. Below are the following ways you can accomplish this:
  1. joining elements into a vector with the contatenate function c().
c(2,4,6)
## [1] 2 4 6
  1. creating an integer sequence.
2:6
## [1] 2 3 4 5 6
  1. creating a complex sequence.
seq(2, 3, by = 0.5)
## [1] 2.0 2.5 3.0
  1. repeating a vector
rep(1:2, times = 3)
## [1] 1 2 1 2 1 2
  1. repeat elements of a vector
rep(1:2, each = 3)
## [1] 1 1 1 2 2 2
  • We can manipulate vectors through a series of functions such as sort(), table(), rev() and unique().

  • sort() will return a sorted version of the inputted vector.

x <- c(1,2,3,4)
sort(x)
## [1] 1 2 3 4
  • table() will provide the number of occurences of each distinct value in a vector.
table(x)
## x
## 1 2 3 4 
## 1 1 1 1
  • rev() will reverse the order of values in the inputted vector.
rev(x)
## [1] 4 3 2 1
  • unique() will output all of the unique values associated with the inputted vector.
unique(x)
## [1] 1 2 3 4
  • We can select vector elements by either position, value or name.
# The fourth element.
x[4]
## [1] 4
# All but the fourth.
x[-4]
## [1] 1 2 3
# Elements two to four.
x[2:4]
## [1] 2 3 4
# All elements except two to four.
x[-(2:4)]
## [1] 1
# Elements one and four.
x[c(1,4)]
## [1] 1 4
# Elements which are equal to 10.
x[x == 10]
## numeric(0)
# All elements less than zero.
x[x < 0]
## numeric(0)
# Elements in the set 1, 2, 5.
x[x %in% c(1, 2, 5)]
## [1] 1 2
# Element with name 'apple'.
x["apple"]
## [1] NA

Programming

  • R can also execute for and while loops along with if statements. In addition, you can create functions.
# A for loop that runs a total of 4 times and consists of creating a variable `j`, whose value is the result of adding 
# the ith value in the current iteration plus 10 and is redefined for each iteration. 
for (i in 1:4){
  j <- i + 10
  print(j)
}
## [1] 11
## [1] 12
## [1] 13
## [1] 14
# Print the value of `i`, add 1 to its value, and assign the corresponding result to the variable `i`; meanwhile, the 
# variable `i`'s value is less than 5.
while (i < 5){
  print(i)
  i <- i + 1
}
## [1] 4
# Print the word "Yes" if the value of `i` is greater than 3; else, print "No"
if (i > 3){
  print("Yes")
} else {
  print("No")
}
## [1] "Yes"
# This function will take an input `x`, compute its squared, store the result in variable `squared`, and lastly return 
# the result. 
square <- function(x){
  squared <- x*x
  return(squared)
}

Reading and Writing Data

  • R gives you the benefit of reading and writing either a delimited text or comma separated value file.
# Storing text file in the object `df`
df <- read.table("file.txt")
# Creating a text file based on what's inside object `df` 
write.table(df, "file.txt")
# Storing csv file in the object `df`
df <- read.csv("file.csv")
# Creating a csv file based on what's inside object `df` 
write.csv(df, "file.csv")

Conditional Statements

a <- 4
b <- 6
a == b # Are equal 
## [1] FALSE
a > b # Greater than 
## [1] FALSE
a >= b # Greater than or equal to 
## [1] FALSE
is.na(a) # Contains an NA element (missing value)
## [1] FALSE
a != b # Not equal 
## [1] TRUE
a < b # Less than 
## [1] TRUE
a <= b # Less than or equal to
## [1] TRUE
is.null(a) # Contains a NULL element (value undefined)
## [1] FALSE