data.frame()
data.frame(..., row.names = NULL, check.rows = FALSE, check.names = TRUE, stringsAsFactors = FALSE) Returns:
data.frame · Updated March 13, 2026 · Data Types data-frame r data-types tabular
A data frame is R’s primary structure for storing rectangular data — like a spreadsheet or SQL table. Each column can hold a different data type (numeric, character, factor, etc.), but all columns must have the same length.
Syntax
data.frame(
col1 = values1,
col2 = values2,
...
)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
... | any | — | Column name = value pairs |
row.names | NULL or character | NULL | Row names (NULL generates automatic names) |
check.rows | logical | FALSE | If TRUE, check that rows are same length |
check.names | logical | TRUE | If TRUE, check that column names are valid |
stringsAsFactors | logical | FALSE | If TRUE, convert character columns to factors |
Examples
Creating a basic data frame
# Create a simple data frame
df <- data.frame(
name = c("Alice", "Bob", "Charlie"),
age = c(25, 30, 35),
score = c(85.5, 92.0, 78.5)
)
df
# name age score
# 1 Alice 25 85.5
# 2 Bob 30 92.0
# 3 Charlie 35 78.5
# Check structure
str(df)
# 'data.frame': 3 obs. of 3 variables:
# $ name : chr "Alice" "Bob" "Charlie"
# $ age : num 25 30 35
# $ score: num 85.5 92 78.5
Accessing columns and rows
# Access column by name
df$name
# [1] "Alice" "Bob" "Charlie"
# Access by position
df[[1]]
# [1] "Alice" "Bob" "Charlie"
# Subset rows
df[1, ]
# name age score
# 1 Alice 25 85.5
# Subset columns
df[, "age"]
# [1] 25 30 35
Adding and modifying columns
# Add a new column
df$city <- c("London", "Paris", "Berlin")
df
# name age score city
# 1 Alice 25 85.5 London
# 2 Bob 30 92.0 Paris
# 3 Charlie 35 78.5 Berlin
# Modify existing column
df$age <- df$age + 1
df
# name age score city
# 1 Alice 26 85.5 London
# 2 Bob 31 92.0 Paris
# 3 Charlie 36 78.5 Berlin
Common Patterns
Converting to tibble (tidyverse)
library(tibble)
as_tibble(df)
# # A tibble: 3 × 4
# name age score city
# <chr> <dbl> <dbl> <chr>
# 1 Alice 26 85.5 London
# 2 Bob 31 92 Paris
# 3 Charlie 36 78.5 Berlin
Reading from CSV
# Read CSV into data frame
df <- read.csv("data.csv", stringsAsFactors = FALSE)
# Same with readr
library(readr)
df <- read_csv("data.csv")