Instructions

Learn how to use each of the follow R Commands by copying the Example Code and pasting it into your R Console.

R Commands Cheatsheet


data( )

Console Help Command: ?data

data()

  • This command lists the available datasets in R. Information about each dataset can then be viewed using the ?nameofthedataset command.

Example Code

#Run these codes in your Console to see what they do.
data()
?cars #Brings up the help file for the cars dataset.
?CO2 #Brings up the help file for the CO2 dataset.


View( )

Console Help Command: ?View

View(object)

  • object is typically the name of a data.frame object in R, i.e., a dataset.

Example Code

View(cars) 
View(CO2)


$

DataSetName$ColumnName

  • The $ operator allows you to access the individual columns of a dataset.

Example Code

View(cars)
cars$speed
cars$dist


mean( )

Console Help Command: ?mean

mean(x)

  • x is a quantitative variable of data.

Example Code

mean(cars$speed)
mean(cars$dist)
Other Stats Functions Example Code
median( ) median(cars$dist)
min( ) min(cars$dist)
max( ) max(cars$dist)
sd( ) sd(cars$dist)
summary( ) summary(cars$dist)
range( ) range(cars$dist)
quantile( ) quantile(cars$dist, .25)
var( ) var(cars$dist)
cor( ) cor(cars$dist, cars$speed)
 

table( )

Console Help Command: ?table

table(x)

  • This function tabulates (or counts) how many observations come from each category of a qualitative variable in a dataset.
  • x is a qualitative variable from a dataset.

Example Code

table(CO2$Type)
table(chickwts$feed)
table(CO2$Type, CO2$Plant) #crosstabs table


hist( )

Console Help Command: ?hist

hist(x)

  • x is a quantitative variable of data.

Example Code

hist(cars$dist)
hist(cars$dist, breaks=10)
hist(cars$dist, main="1920's Vehicles", xlab="Stopping Distance")
Other Stats Plots Example Code
boxplot( ) boxplot(cars$dist)
stripchart( ) stripchart(cars$dist)
qqnorm( ) qqnorm(cars$dist); qqline(cars$dist)
barplot( ) barplot(table(chickwts$feed))
 

plot( )

Console Help Command: ?plot

plot(y ~ x, data=NameOfDataset)

  • y is the quantitative variable for the y-axis.
  • x is the quantitative variable for the x-axis.
  • NameOfDataset is the name of the dataset that contains y and x as columns.

Example Code

plot(dist ~ speed, data=cars)
plot(dist ~ speed, data=cars, pch=16)
plot(dist ~ speed, data=cars, pch=16, col="skyblue")
plot(dist ~ speed, data=cars, pch=8, col="firebrick")
plot(dist ~ speed, data=cars, pch=8, col="firebrick")

pch Options

col Options

#Run this command in your Console 
colors() #available colors in R


with( )

Console Help Command: ?with

with(datasetName, commands)

  • This function allows you to not have to use $ to access the columns of a dataset. It is useful sometimes, but not always.
  • datasetName is the name of a dataset like cars or CO2.
  • commands are any R Commands that need to be performed using the variables within the specified dataset.

Example Code

with(cars, mean(dist)) #same thing as: mean(cars$dist)
with(cars, cor(dist, speed)) #same as: cor(cars$dist, cars$speed)


subset( )

Console Help Command: ?subset

subset(NameOfDataset, condition)

  • NameOfDataset is the name of a dataset, like cars or CO2.
  • condition is some logical expression that uses any of the following logical connectors.
Logical Expression Syntax
Equals ==
Not Equal !=
Less Than <
Less Then or Equal to <=
Greater Than >
Greater Than or Equal to >=
AND &
OR |
IN %in%
NOT !

Example Code

subset(CO2, Type=="Quebec")
subset(CO2, Type!="Quebec")
subset(CO2, conc < 175)
subset(CO2, conc <= 175)
subset(CO2, conc > 675)
subset(CO2, conc >= 675)
subset(CO2, Type=="Quebec" & conc <= 175)
subset(CO2, Plant=="Qn1" | Plant=="Mc2")
subset(CO2, Plant %in% c("Qn1","Qc3","Mn2","Mc3"))
subset(CO2, Type !="Quebec")


<-

Console Help Command: none

NameYouCreate <- some R commands

  • <- (Less than symbol < with a hyphen -) is called the assignment operator and lets you store the results of the some R commands into an object called NameYouCreate.
  • NameYouCreate is any name that begins with a letter, but can use numbers, periods, and underscores thereafter. The name should not contain spaces.

Example Code

aveSpeed <- mean(cars$speed)
aveSpeed

fastCars <- subset(cars, speed > 20)
View(cars)
View(fastCars)

fastCars$NewColumnFTperSEC <- fastCars$speed * 5280 / 3600
View(fastCars)


c( )

Console Help Command: ?c

c( )

  • The c( ) function combines values into a vector or list.

Example Code

c(1,3,5)
c(1:5)
c("dog","cat","bird")

animals <- c("dog","cat","bird")
animals

counts <- c(5, 2, 18)
counts


[ ]

Console Help Command: none

objectName[ elementNumber ]

objectName[ Rows, Columns]

  • If objectName is an object that contains a list of numbers then [ elementNumber ] will pull out the specified elements.
  • If objectName is an object with rows and columns (like a dataset) then the specified [ Rows , Columns] can be accessed.

Example Code

counts <- c(5, 2, 18)
counts
counts[3]   #third element
counts[2:3]  #second through third elements
counts[c(1,3)] #first and third elements

View(cars)
cars[2:3, ] #second and third rows, all columns
cars[ ,2]   #all rows, second column
cars[2,2]   #second row, second column (single item)
cars[2:3,2] #second and third rows, second column