args(data.frame)function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,
fix.empty.names = TRUE, stringsAsFactors = FALSE)
NULL
Practise this interactively in Course 1 (Introduction to R), lesson 7
Learning Objectives: After this chapter you will be able to:
- Create a data frame from equal-length vectors.
- Inspect one with
str(),dim(),names(), andnrow()/ncol().- Select columns with
[,[[, and$.- Select rows by position and by condition.
- Subset rows and columns independently.
- Explain why
stringsAsFactorsdefaults toFALSEin R 4.0.0 and later.
Data frames are lists of equal-length vectors, so they can hold columns of different types.
Use data.frame to create data frames. The function syntax is:
function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,
fix.empty.names = TRUE, stringsAsFactors = FALSE)
NULL
Data frames are lists with elements of equal length, so they can hold different data types. Let us create a data frame:
class(students)
## [1] "data.frame"
names(students)
## [1] "name" "age" "graduate"
colnames(students)
## [1] "name" "age" "graduate"
str(students)
## 'data.frame': 3 obs. of 3 variables:
## $ name : chr "John" "Jack" "Jill"
## $ age : num 29 25 27
## $ graduate: logi TRUE TRUE FALSE
dim(students)
## [1] 3 3
nrow(students)
## [1] 3
ncol(students)
## [1] 3[][[]]$Edit the code below and press Run. It executes entirely in your browser via WebR β no R installation needed. The runtime downloads once, on this page only.
First run downloads the R runtime and any required packages (about 20 s); later runs reuse them.
Starting with R 4.0.0 (April 2020), the default value of stringsAsFactors in data.frame() is FALSE, meaning character columns remain as character type. In older versions of R (< 4.0.0), the default was TRUE, which automatically converted character vectors to factors.
Common Mistake: Assuming
data.frame()still converts strings to factors. That was the default before R 4.0.0 and was a frequent source of confusion. On R >= 4.0.0 the default isFALSE.
Knowledge Check: One column, three operators What do students[1], students[[1]], and students$name each return?
Answer: A one-column data frame; a vector; and the same vector.
Explanation:students[1], with a single index and no comma, always returns a data frame, even for a single column, so downstream code that expects a vector breaks. [[ and $ both extract the column itself as a vector. Reach for [[ or $ whenever you want the values.
Knowledge Check: Rows and columns are indexed separately What does students[1, ] return, and how does that differ from students[, 1]?
Answer: students[1, ] returns the first row; students[, 1] returns the first column. The comma is what separates the two.
[ the first position selects rows and the second selects columns. Leaving one side empty means βall of themβ. So students[1, ] is row 1, students[, 1] is column 1, and students[1, 1] is the single cell where they cross.
Knowledge Check: Filtering rows by a condition How do you return only the students older than 26?
Answer: students[students$age > 26, ], which gives John (29) and Jill (27).
TRUE and drops the rest. The column position after the comma is empty, so every column is kept.
Knowledge Check: How many rows and columns? The data frame has three columns and three rows. Which two functions report those counts directly?
Answer: nrow(students) gives 3 and ncol(students) gives 3. Both are also available together as dim(students), which returns c(3, 3).
dim() returns rows first, then columns, so dim(students)[1] is the row count. str() gives you a structural overview while nrow() and ncol() answer the two specific questions directly.
Knowledge Check: stringsAsFactors changed in R 4.0.0 In R 4.0.0 and later, what does data.frame(name, age, graduate) do with the character column name?
Answer: It keeps it as character. Before R 4.0.0 the default was TRUE, which silently converted character columns to factors.
stringsAsFactors = FALSE is now redundant on R >= 4.0.0, but it keeps code readable and portable to older R.
str(), dim(), nrow() and ncol() to inspect its structure.[[ or $ to extract a column as a vector; students[1] keeps a data frame.students[rows, columns], the comma separates row and column selections.Next: Install & Update R Packages
Practise interactively: Course 1, lesson 7