Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

Data Frames in R: Create, Inspect and Use

What a data frame is in R, how to create one with data.frame() or read.csv(), inspect it with head() and str(), and avoid R 4.0 and check.names surprises.

Written and maintained by CASRAI Editorial Board

Last updated

A data frame in R is a list of variables that all have the same number of rows, stored with the class “data.frame”. Its columns can be of different types (numeric, logical, factor, character), which is why it is the usual container for a research dataset: one row per observation, one column per variable. This guide covers what R’s own documentation says a data frame is, how to create and inspect one, and the version-specific behaviours that older tutorials still get wrong.

What a data frame is used for in research

The R manual states that a data frame is a list with class “data.frame”, and the data.frame help page calls the result a matrix-like structure “whose columns may be of differing types”. The same page says data frames are “used as the fundamental data structure by most of R’s modeling software”. In practice that means the table you load from a survey export, an instrument file or a spreadsheet is a data frame before you summarise, plot or model it. See what R is used for for the wider picture.

R programming data frame versus a matrix

The manual notes that a data frame can for many purposes be regarded as a matrix with columns of possibly differing modes and attributes. A matrix holds one type throughout; a data frame lets a numeric measurement sit next to a character participant label.

Install or access (official source only)

Data frames are part of base R, so there is nothing extra to install: download R from the Comprehensive R Archive Network (CRAN) at cran.r-project.org. If you prefer an editor, RStudio works with the same commands. The tibble package, a variant of the data frame, is installed from CRAN with:

install.packages("tibble")

Step-by-step: create and inspect a data frame

Data frame in R programming: building one with data.frame()

  1. Create one vector per variable, all the same length (or a length that divides evenly into the longest, see Troubleshooting).
  2. Pass the vectors to data.frame(), naming each column:
    trial <- data.frame(
      id = 1:4,
      group = c("control", "treated", "control", "treated"),
      score = c(12.5, 14.1, 11.8, 15.3)
    )
  3. Look at the first rows with head(trial). Per the utils help page, head() and tail() return the first or last parts of a data frame, and the default is n = 6.
  4. Check the shape with nrow(trial) and ncol(trial), which return the number of rows and columns.
  5. Inspect the structure with str(trial), which the R manual describes as a compact display of the internal structure of an object, with an S3 method for data frames.

Reading a data frame from a file

The R introduction says the simplest way to construct a data frame from scratch is to read it from a file with read.table(), which reads an entire data frame from an external file. read.csv() is the comma-separated variant with the same result type, a data frame; read.csv2() covers the semicolon-separated variant used where a comma is the decimal mark. A worked file-import walkthrough is in how to load data into R.

Selecting a column

Because a data frame is a list, its components are reached the way list components are: trial$score or trial[["score"]]. The manual notes that Lst$name is the same as Lst[["name"]], and that the double-bracket form is the one to use when the name is held in a variable.

Output and how to read it

Printing a data frame, or its first rows with head(), shows a table: the column names across the top and one row per observation, with an automatic row label at the left. When you do not supply row names, R takes them from the first component with suitable names, and otherwise uses the integers from 1; the help page calls those “automatic”. For summary figures per column, continue with summary statistics in R, and to add columns see cbind in R.

Troubleshooting

Columns of different lengths

The manual requires that vector variables in a data frame all have the same length, and that matrix variables have the same number of rows. The help page adds that atomic vectors, factors and protected character vectors are recycled a whole number of times if necessary, so a length-2 column can fill a length-4 frame but a length-3 column cannot. Fix the shorter vector before building the frame.

Column names that change

With check.names = TRUE (the default), names are checked to be syntactically valid and not duplicated, and are adjusted with make.names if necessary. A header such as a name containing a space is therefore altered on the way in. Set check.names = FALSE to keep it, but note the help page warns that not all operations preserve duplicated names.

Text turning into factors (or not)

The default for stringsAsFactors is FALSE. The help page records that the earlier “factory-fresh” default was TRUE and “has been changed to FALSE for R 4.0.0”. Tutorials written before that release may show character columns arriving as factors; on current R they arrive as character unless you ask for factors.

Free alternatives

Option What the official documentation says Where it comes from
Base R data.frame Tightly coupled collections of variables; matrix-like, columns of differing types Included with R (CRAN)
read.table() / read.csv() Reads a file in table format and creates a data frame from it Included with R (utils package)
tibble Data frames that are “lazy and surly”: they do less, do not change variable names or types, and do not do partial matching. as_tibble() converts an existing object; tibble() builds one from columns install.packages("tibble")

For reproducible work, the practical difference is that a tibble does not rename or retype your columns on creation, whereas data.frame() applies the check.names rule above. If you are comparing R with other statistics tools, see SPSS vs R.

FAQ

What is a data frame in R programming?

It is a list of variables with the same number of rows and the class “data.frame”, usually displayed as a table whose columns may hold different types.

How do I create a data frame in R?

Call data.frame() with one named vector per column, or read a file with read.table() or read.csv().

Is a data frame the same as a list?

A data frame is a list with an added class and the restriction that its components be same-length vectors (or matching-row matrices, lists or data frames), so list-style access with $ and [[ ]] works on it.

How many rows does head() show?

Six by default (n = 6), for a data frame as for a vector or matrix.

What is the difference between a data frame and a tibble?

A tibble is a data frame that, per the tibble documentation, does less: it does not change variable names or types and does not do partial matching.

Sources (official docs)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Data Frames in R: Create, Inspect and Use

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →