Nathan Byers
April 21, 2014
Now that we know some basics, we'll cover
data <- read.csv(file = "C:/Documents/mydata.csv")
data
will be a data frameClass | Example |
---|---|
character | c("red", "white", "blue") |
integer | c(1, 2, 3) |
numeric | c(1.5, 2.9, 3.3) |
logical | c(TRUE, FALSE, TRUE) |
x <- c("red", "white", "blue")
x[2]
[1] "white"
matrix()
function1:3
creates the vector c(1, 2, 3)
)m <- matrix(data = 1:12, nrow = 3)
m
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[rows, columns]
m[, 1]
[1] 1 2 3
m[2, ]
[1] 2 5 8 11
m[3, 4]
[1] 12
m[c(2, 3), c(3, 4)]
[,1] [,2]
[1,] 8 11
[2,] 9 12
Here's the example from part 1:
price <- c(1000, 4000, 2000, 5000, 500)
carat <- c(0.4, 0.55, 0.45, 0.65, .2)
color <- c("G", "H", "D", "E", "G")
diamonds <- data.frame(price, carat, color)
diamonds
price carat color
1 1000 0.40 G
2 4000 0.55 H
3 2000 0.45 D
4 5000 0.65 E
5 500 0.20 G
diamonds
data framediamonds[c(4, 5), 1]
[1] 5000 500
diamonds[c(4, 5), "price"]
[1] 5000 500
$
symboldiamonds$price
[1] 1000 4000 2000 5000 500
sales <- list(product = diamonds, store = "downtown", month = "january")
sales
$product
price carat color
1 1000 0.40 G
2 4000 0.55 H
3 2000 0.45 D
4 5000 0.65 E
5 500 0.20 G
$store
[1] "downtown"
$month
[1] "january"
function()
and take arguments in the parenthesismatrix()
function, run?matrix()
matrix()
are data
, nrow
, ncol
, byrow
, and dimnames
matrix(data = 1:12, nrow = 3)
is equivalent to
matrix(1:12, 3)