How to create dataframe in R

Last Updated : 24 Jul, 2025

Dataframes are basic structures in R used to store and work with data in table format. They help us arrange data in rows and columns, like a spreadsheet or database table. We can use different ways to make dataframes while working with data in R.

1. Creating and Combining Vectors Using data.frame

We make vectors for each column and then combine them into a dataframe using the data.frame function.

  • data.frame: Used to create a dataframe by combining vectors.
  • c: Used to create vectors by combining values.
  • print: Used to display the content of the dataframe.
R
name <- c("Johny", "Ali", "Boby", "Emilya")
age <- c(25, 30, 22, 28)
salary <- c(50000, 60000, 45000, 55000)
df <- data.frame(Name = name, Age = age, Salary = salary)
print(df)

Output:

dataset
Output

In this example, we create vectors for name, age and salary and then use the data.frame() function to combine them into a dataframe called df.

2. Reading Data from File Using read.table or read.csv

We can create a dataframe by reading tabular data from external files using read.table or read.csv.

  • read.table: Reads data from a text file and makes a dataframe.
  • sep: Sets the separator used in the file like tab or comma.
  • header: If set to true, uses the first line of the file as column names.

df <- read.table("data.tsv", sep = "\t", header = TRUE)

3. Creating Tibble Using tibble Package

We can also create a dataframe using the tibble package, which gives some extra features and cleaner output.

  • install.packages: Installs the tibble package.
  • library: Loads the tibble package to use its functions.
  • tibble: Used to make a tibble, a modern version of dataframe.
  • print: Shows the tibble on the console.
R
install.packages("tibble")
library(tibble)
df <- tibble(
  Name = c("Johny", "Ali", "Boby", "Emilya"),
  Age = c(25, 30, 22, 28),
  Salary = c(50000, 60000, 45000, 55000)
)
print(df)

Output:

dataframe
Output

In this example, we use the tibble() function from the tibble package to create a tibble named df.

We use dataframes in R to store and organize data for analysis. We can create them by combining vectors, reading files, or using the tibble package. Each method is useful based on the type and source of our data.

Comment

Explore