A data frame is a tabular structure—rows are observations, columns are variables. It is the R equivalent of a spreadsheet or pandas DataFrame in Python.
Creating data frames
df <- data.frame(
name = c("Ada", "Lin", "Sam"),
score = c(92, 88, 95),
passed = c(TRUE, TRUE, TRUE)
)
print(df)
print(nrow(df))
print(ncol(df))
Column access
print(df$name)
print(df[["score"]])
print(df[1, ])
$ and [[ ]] extract columns; [row, col] subsets rows and columns.
Important interview questions and answers
- Q: data.frame vs matrix?
A: Data frames hold mixed column types; matrices are homogeneous 2D numeric/character. - Q: Why stringsAsFactors changed?
A: Older R coerced strings to factors by default; modern R keeps character columns unless requested.
Self-check
- How many rows does nrow(df) return?
- How do you access the score column with $?
Tip: data.frame is the hub—compare with pandas after your Python track.
Interview prep
- data.frame vs matrix?
Data frames allow mixed column types; matrices are homogeneous 2D.