Add custom notes to the My Quick R Notes section below.


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

#Run these codes in your Console to see what they do.

View(cars) #Shows the entire cars dataset
View(CO2) #Shows the entire CO2 dataset


$

DataSetName$ColumnName

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

Example Code

#Run these codes in your Console to see what they do.

View(cars) #Shows the entire cars dataset
cars$speed #Access the speed column from the cars dataset
cars$dist #Access the dist column from the cars dataset


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

#Run these codes in your Console to see what they do.

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

#Run these codes in your Console to see what they do.

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

#Run these codes in your Console to see what they do.

aveSpeed <- mean(cars$speed) #Saves the mean into the aveSpeed object
aveSpeed #prints the saved mean to the screen

fastCars <- subset(cars, speed > 20) #Saves the subset into fastCars
View(cars) #Shows the entire cars dataset
View(fastCars) #Shows the entire fastCars dataset

fastCars$NewColumnFTperSEC <- fastCars$speed * 5280 / 3600 #Add new column
View(fastCars) #See the newly expanded fastCars dataset


c( )

Console Help Command: ?c

c( )

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

Example Code

#Run these codes in your Console to see what they do.
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

#Run these codes in your Console to see what they do.

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



My Quick R Notes

Add any R commands you use and want to remember later into the following chunk.

round(1.55324, 2) #Rounds numbers to 2 decimal places
class(cars) #Shows the R "class" of cars
class(cars$speed) #Shows the R "Class" of cars$speed
str(cars) #Shows the R "structure" of an object