Base R Cheat Sheet

ADVERTISEMENT

Base R
Vectors
Programming
For Loop
While Loop
Creating Vectors
Cheat Sheet
Join elements into
for
(variable
in sequence){
while (condition){
c(2, 4, 6)
2 4 6
a vector
Do something
Do something
Getting Help
An integer
2:6
2 3 4 5 6
sequence
}
}
Accessing the help files
Example
A complex
Example
seq(2, 3, by=0.5)
2.0 2.5 3.0
sequence
for (i in 1:4){
while (i < 5){
?mean
Get help of a particular function.
rep(1:2, times=3)
1 2 1 2 1 2
Repeat a vector
j <- i + 10
print(i)
help.search(‘weighted
mean’)
print(j)
i <- i + 1
Search the help files for a word or phrase.
Repeat elements
rep(1:2, each=3)
1 1 1 2 2 2
of a vector
help(package = ‘dplyr’)
}
}
Find help for a package.
Vector Functions
More about an object
If Statements
Functions
sort(x)
rev(x)
Return x sorted.
Return x reversed.
function_name
<- function(var){
if (condition){
str(iris)
table(x)
unique(x)
Do something
Get a summary of an object’s structure.
Do something
} else {
See counts of values.
See unique values.
class(iris)
Do something different
return(new_variable)
Find the class an object belongs to.
}
}
Selecting Vector Elements
Using Libraries
Example
Example
By Position
square <- function(x){
if (i > 3){
install.packages(‘dplyr’)
x[4]
print(‘Yes’)
The fourth element.
squared <- x*x
Download and install a package from CRAN.
} else {
print(‘No’)
return(squared)
x[-4]
All but the fourth.
library(dplyr)
}
}
Load the package into the session, making all
its functions available to use.
x[2:4]
Elements two to four.
Reading and Writing Data
dplyr::select
All elements except
Input
Ouput
Description
x[-(2:4)]
Use a particular function from a package.
two to four.
Read and write a delimited text
Elements one and
df <- read.table(‘file.txt’)
write.table(df, ‘file.txt’)
data(iris)
x[c(1, 5)]
file.
five.
Load a built-in dataset into the environment.
By Value
Read and write a comma
Working Directory
separated value file. This is a
Elements which
df <- read.csv(‘file.csv’)
write.csv(df, ‘file.csv’)
x[x == 10]
special case of read.table/
are equal to 10.
write.table.
getwd()
All elements less
x[x < 0]
Find the current working directory (where
than zero.
Read and write an R data file, a
inputs are found and outputs are sent).
load(‘file.RData’)
save(df, file = ’file.Rdata’)
file type special for R.
x[x %in%
Elements in the set
c(1, 2, 5)]
1, 2, 5.
setwd(‘C://file/path’)
Change the current working directory.
Named Vectors
Greater than
a == b
a > b
a >= b
is.na(a)
Are equal
Greater than
Is missing
Conditions
or equal to
Use projects in RStudio to set the working
Element with
Less than or
x[‘apple’]
a != b
a < b
a <= b
is.null(a)
Not equal
Less than
Is null
directory to the folder you are working in.
equal to
name ‘apple’.
RStudio® is a trademark of RStudio, Inc. •
CC BY
Mhairi McNeill •
Learn more at web page or vignette • package version • Updated: 3/15

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2