How to Create a DataFrame in R | The School of Code

Settings

Appearance

Choose a typography theme that suits your style

Back to How-to Guides
R

How to Create a DataFrame in R

Learn how to create and manipulate DataFrames in R using the data.frame() function and tibbles from the tidyverse package.

RDataFramesData ScienceStatistics

DataFrames are one of the most important data structures in R for data analysis. They allow you to store tabular data with different column types. In this guide, you’ll learn multiple ways to create DataFrames in R.

Method 1: Using data.frame()

The most common way to create a DataFrame in R is using the built-in data.frame() function:

# Create a simple DataFrame
df <- data.frame(
  name = c("Alice", "Bob", "Charlie"),
  age = c(25, 30, 35),
  city = c("New York", "London", "Tokyo")
)

# View the DataFrame
print(df)

This creates a DataFrame with three columns: name, age, and city.

Method 2: Using tibbles (tidyverse)

If you’re using the tidyverse ecosystem, you can create tibbles which are modern DataFrames with better printing and subsetting behavior:

library(tibble)

# Create a tibble
df <- tibble(
  name = c("Alice", "Bob", "Charlie"),
  age = c(25, 30, 35),
  city = c("New York", "London", "Tokyo")
)

print(df)

Method 3: Reading from a CSV file

You can also create a DataFrame by reading data from a CSV file:

# Using base R
df <- read.csv("data.csv")

# Using readr (tidyverse) for faster reading
library(readr)
df <- read_csv("data.csv")

Checking Your DataFrame

After creating a DataFrame, you can inspect it with these useful functions:

# View structure
str(df)

# View first few rows
head(df)

# Get dimensions
dim(df)

# Column names
names(df)

Summary

Creating DataFrames in R is straightforward:

  • Use data.frame() for base R
  • Use tibble() for tidyverse workflows
  • Use read.csv() or read_csv() for loading external data

Now you’re ready to start working with tabular data in R!