R Data Frames
A data frame is a table: each column is a vector (so it has one type), and different columns can have different types. It is the structure you'll use for almost all real data in R, much like a spreadsheet or a SQL table.
team <- data.frame(
name = c("Ana", "Ben", "Cai", "Dee"),
role = c("dev", "design", "dev", "pm"),
years = c(5, 2, 8, 3),
remote = c(TRUE, FALSE, TRUE, TRUE)
)
team name role years remote
1 Ana dev 5 TRUE
2 Ben design 2 FALSE
3 Cai dev 8 TRUE
4 Dee pm 3 TRUE
The numbers on the left are row names; by default they are just 1, 2, 3, …
Inspecting a data frame
team <- data.frame(
name = c("Ana", "Ben", "Cai", "Dee"),
role = c("dev", "design", "dev", "pm"),
years = c(5, 2, 8, 3),
remote = c(TRUE, FALSE, TRUE, TRUE)
)
dim(team)
names(team)
str(team)
summary(team$years)[1] 4 4
[1] "name" "role" "years" "remote"
'data.frame': 4 obs. of 4 variables:
$ name : chr "Ana" "Ben" "Cai" "Dee"
$ role : chr "dev" "design" "dev" "pm"
$ years : num 5 2 8 3
$ remote: logi TRUE FALSE TRUE TRUE
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.00 2.75 4.00 4.50 5.75 8.00
| Function | Shows |
|---|---|
head(df) / tail(df) |
first / last 6 rows |
nrow(df), ncol(df), dim(df) |
size |
names(df) / colnames(df) |
column names |
str(df) |
type and first values of each column |
summary(df) |
statistics for every column |
Selecting columns and rows
A data frame is indexed like a matrix, df[rows, columns], and its columns can also be reached like list elements with $:
team <- data.frame(
name = c("Ana", "Ben", "Cai", "Dee"),
role = c("dev", "design", "dev", "pm"),
years = c(5, 2, 8, 3),
remote = c(TRUE, FALSE, TRUE, TRUE)
)
team$name
team[2, ] # second row
team[, c("name", "years")] # two columns
team[team$years > 4, ] # rows matching a condition
team[team$role == "dev", "name"][1] "Ana" "Ben" "Cai" "Dee"
name role years remote
2 Ben design 2 FALSE
name years
1 Ana 5
2 Ben 2
3 Cai 8
4 Dee 3
name role years remote
1 Ana dev 5 TRUE
3 Cai dev 8 TRUE
[1] "Ana" "Cai"
Adding and changing columns
team <- data.frame(name = c("Ana", "Ben", "Cai"), years = c(5, 2, 8))
team$senior <- team$years >= 5
team$salary <- c(68000, 52000, 81000)
team$salary <- team$salary * 1.03 # a 3% raise for everyone
team name years senior salary
1 Ana 5 TRUE 70040
2 Ben 2 FALSE 53560
3 Cai 8 TRUE 83430
Adding rows
rbind() appends rows; the new rows must have the same columns:
team <- data.frame(name = c("Ana", "Ben"), years = c(5, 2))
new_hire <- data.frame(name = "Eli", years = 0)
team <- rbind(team, new_hire)
team
nrow(team) name years
1 Ana 5
2 Ben 2
3 Eli 0
[1] 3
Sorting and summarising
order() returns the row order; pass it as the row index to sort. Columns are ordinary vectors, so every vector function works on them:
team <- data.frame(
name = c("Ana", "Ben", "Cai", "Dee"),
role = c("dev", "design", "dev", "pm"),
years = c(5, 2, 8, 3)
)
team[order(team$years, decreasing = TRUE), ]
mean(team$years)
table(team$role) name role years
3 Cai dev 8
1 Ana dev 5
4 Dee pm 3
2 Ben design 2
[1] 4.5
design dev pm
1 2 1
Built-in data frames
R ships with many data frames for practice. mtcars (cars), iris (flowers), airquality (New York weather) and ToothGrowth are used throughout this course:
head(iris, 3)
nrow(iris)
colMeans(iris[, 1:4]) Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
[1] 150
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.843333 3.057333 3.758000 1.199333
Tip: The Data Frame Manipulation lesson later covers
subset(),merge(),aggregate(),with()andtransform().
Test yourself
Can different columns of a data frame have different types?
- No, like a matrix
- Yes, each column is its own vector
- Only numeric and integer
- Only with factors
What does df[df$age > 30, ] return?
- The age column
- The rows where age > 30, all columns
- A logical vector
- The number of rows
Which function shows each column's type and first few values?
- dim()
- str()
- names()
- nrow()