Chapter 19 Gaining more familiarity with R
19.1 Variables and values
Variables are created by assigning a name to a value. Names of variables are case-sensitive and can include letters, numbers, “_” and “.” but cannot begin with a number. Below we demonstrate how to create two variables, “x”, and “z”. Let “x” denote the number of chocolate chips claimed by a bakery to be in a biscuit purchased, and “z” the number of chocolate chips actually in the biscuit.
<- 8 # x is assigned the value 8
x #x # The value of x, the number of chocolate chips advertised
<-5 # z is assigned the value 5
z#z # the value of chocolate chips actually found in the biscuit
19.1.1 “Assignment” creates a copy of a value.
<- x # y is assigned the value of x
y <- 0 # y becomes 0, x is not changed by this
y <- x + 1 # y becomes x+1, x is not changed by this
y <- x + 1 # x becomes x+1, its previous value is lost
x # the number of choclate chips advertised is now 9!
19.1.2 Vectors
A numeric vector is an ordered collection of numbers (see “biscuitTin” and “y”below). Here are some examples of how we can manipulate vectors.
<- c(1, 3.14, 0, -3, 7) # Function c combines elements into vectors.
biscuitTin # Lets imagine that each element in bisuitTin is the number of chocolate chips in each of five biscuits.
<- 1:5 # : makes a numeric sequence in integer steps, 1,2,3,4,5
y ^2 # Square each element...essentially we have each element in biscuitTin squared :-) biscuitTin
## [1] 1.0000 9.8596 0.0000 9.0000 49.0000
+ 1 # Add 1 to each element in biscuitTin. (The 1 is recycled) biscuitTin
## [1] 2.00 4.14 1.00 -2.00 8.00
+ y # Add corresponding pairs of elements biscuitTin
## [1] 2.00 5.14 3.00 1.00 12.00
1] # Get the first element of biscuitTin biscuitTin[
## [1] 1
1:3] # Get the first three elements in biscuitTin biscuitTin[
## [1] 1.00 3.14 0.00
#
# # Negative numbers index elements "not" indexed.
-1] # Get all except the first element. Think of it as removing the first element. biscuitTin[
## [1] 3.14 0.00 -3.00 7.00
-(1:3)] # Get all except the first three elements biscuitTin[
## [1] -3 7
#
# # Indexing and assignment.
#
1:3] <- c(7,8,9) # Set the values for first three elements
biscuitTin[1:3] <- 0 # Set the first three elements to 0 (recycled)
biscuitTin[-c(1:3)] <- 1 # Set all except the first three elements to 1
biscuitTin[
> 0] # Get elements where biscuitTin>0 is TRUE biscuitTin[biscuitTin
## [1] 1 1
<- biscuitTin[biscuitTin > 0] # Drop elements where biscuitTin<=0
biscuitTin < 5] = 0 # Set elements to 0 (recycled) where biscuitTin<5 is TRUE biscuitTin[biscuitTin
19.2 Some more summary functions
Below are some summary functions performed on our biscuitTin as an example.
# length(biscuitTin)
# min(biscuitTin)
# max(biscuitTin)
# sum(biscuitTin)
# mean(biscuitTin)
# sd(biscuitTin)
# var(biscuitTin)
19.3 Dataframes in R
R includes several data sets. One of the data sets is named iris. Below, we have written some code to help explore the iris dataset. Try these out and see if you can determine what each function does. Iris is a “data frame” with 150 cases (rows) and 5 variables (columns). Notes that the columns of a data frame are vectors or factors.
# data()
# help(iris) # What are the details on this dataset?
# class(iris) # What class of object is this?
# dim(iris) # Dimensions (number of rows and columns)
# names(iris) # Names of the variables (columns)