Recall that a vector is a one-dimensional array of values.
A matrix is a two-dimensional array of values consisting of rows and columns. For example, each column is a vector.
A dataframe (or table) is similar to a list in that it can contain different types of values, like text (character) and numbers (double).
When we do calculations on data, we have to make sure that the data is all one type, usually numeric.
To load one of R’s internal datasets, we “call” it.
data(mtcars)
View is a nice way to take a look at more complex
objects.
View(mtcars)
Ugh. What is a “drat”?
help(mtcars)
How do we find the size of a matrix? There are two lengths, right? The number of rows and the number of columns.
dim(mtcars)
## [1] 32 11
I can never remember if rows or columns come first.
R has functions for that!
nrow(mtcars)
## [1] 32
ncol(mtcars)
## [1] 11
What if we don’t like the way the matrix is put together? Maybe I
want the rows as columns and the columns as rows. Is there a function
for that? Of course. t() is the transverse function.
flipped_mtcars <- t(mtcars)
View(flipped_mtcars)
What are the names of the columns of mtcars again?
colnames(mtcars)
## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
## [11] "carb"
Hmmmm. Let’s see if there is a relationship between “mpg” and “wt”.
Using the operator $, we can pull out columns by their
names.
plot(mtcars$wt, mtcars$mpg)
This is often easier than figuring out what columns the variables live in.
plot(mtcars[,6], mtcars[,1])
With matrices, we can subset as we did with vectors. But we have to keep in mind that we are subsetting on two dimension (x and y).
mtcars[1,1]
## [1] 21
mtcars[14,20]
## NULL
Oops!
dim(mtcars)
## [1] 32 11
Be careful you don’t refer to something that isn’t there!
The plot() function takes two arguments. Think of it as
“plot x versus y.”
plot(mtcars$gear, mtcars$mpg)
Does our flipped (transverse) matrix have column names?
colnames(flipped_mtcars)
## [1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
## [4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
## [7] "Duster 360" "Merc 240D" "Merc 230"
## [10] "Merc 280" "Merc 280C" "Merc 450SE"
## [13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood"
## [16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128"
## [19] "Honda Civic" "Toyota Corolla" "Toyota Corona"
## [22] "Dodge Challenger" "AMC Javelin" "Camaro Z28"
## [25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2"
## [28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino"
## [31] "Maserati Bora" "Volvo 142E"
plot(flipped_mtcars[,"Cadillac Fleetwood"], flipped_mtcars[,"Maserati Bora"])
It looks like they are somewhat similar, but we can’t really tell because the scales for the different features are so different.
range(mtcars$carb)
## [1] 1 8
range(mtcars$disp)
## [1] 71.1 472.0
We will revisit this issue of scaling when we work with the gene expression data.
Hey, I don’t see my car in this matrix!! Oh, that’s because the data are from 1981 (check out the help file).
But R will let me add my car’s data.
honda_fit <- c(33, 4, 92, 128, 4.62, 2.568, 16.4, 0, 0, 6, 1)
my_mtcars <- rbind(mtcars,honda_fit)
View(my_mtcars)
Notice that I made a new matrix my_mtcars. I don’t trust
myself not to make a mistake (and often in programming mistakes are good
because we can learn a lot from them), so I make a new object that has
my changes. mtcars is still mtcars.
This is interesting: I can use subsetting on the row.names to add the name of my car!
row.names(my_mtcars)[33] <- "Honda Fit"
View(my_mtcars)
Let’s check out how things have changed.
dim(mtcars)
## [1] 32 11
dim(my_mtcars)
## [1] 33 11
We can also add a new column! We have some new data- ratings for the cars.
This is our first time reading in new data. It’s pretty exciting.
A .csv file is a “comma-separated” file. Data often come in files of this format. Commas often serve as a convenient way to delineate the fields in a file.
rating <- read.csv("rating.csv",header=TRUE)
head(rating)
## rating
## 1 3
## 2 3
## 3 2
## 4 1
## 5 2
## 6 2
Well, it looks like this data is ready to be added to my matrix
my_mtcars. It even has a name.
The R function cbind() lets us bind a column to our matrix. How nice is that?!
my_mtcars_ratings <- cbind(my_mtcars,rating)
View(my_mtcars_ratings)
I want to save the new matrix to a file.
Remember to include the object in the write.csv()
function.
write.csv(my_mtcars_ratings, "mtcars_HondaFit_ratings.csv")