Open In App

How to create dataframe in R

Last Updated : 18 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Dataframes are fundamental data structures in R for storing and manipulating data in tabular form. They allow you to organize data into rows and columns, similar to a spreadsheet or a database table. Creating a data frame in the R Programming Language is a simple yet essential task for data analysis and manipulation. In this article, we’ll explore different methods to create data frames in R.

Create a data frame Using the data.frame() Function

The most common method to create a data frame in R is by using the data.frame() function. This function takes vectors as input and combines them into a data frame.

R
# Creating vectors
name <- c("Johny", "Ali", "Boby", "Emilya")
age <- c(25, 30, 22, 28)
salary <- c(50000, 60000, 45000, 55000)
# Creating a dataframe
df <- data.frame(Name = name, Age = age, Salary = salary)
# Displaying the dataframe
print(df)

Output:

    Name Age Salary
1  Johny  25  50000
2    Ali  30  60000
3   Boby  22  45000
4 Emilya  28  55000

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.

Create a data frame Using the read.table() or read.csv() Functions

Another way to create a dataframe in R is by reading data from an external file using functions like read.table() or read.csv(). These functions read data from text files (such as CSV files) and automatically convert them into dataframes. Here’s an example using read.csv():

Read data from a tab-separated file

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

data.csv is a CSV file containing tabular data. The read.csv() function reads this file and creates a dataframe df.

Create a data frame Using the tibble Package

The tibble package provides an alternative to the base R dataframe with some additional features. You can create a tibble using the tibble() function. Here’s an example:

R
# Installing and loading the tibble package
install.packages("tibble")
library(tibble)
# Creating a tibble
df <- tibble(
  Name = c("Johny", "Ali", "Boby", "Emilya"),
  Age = c(25, 30, 22, 28),
  Salary = c(50000, 60000, 45000, 55000)
)
# Displaying the tibble
print(df)

Output:

# A tibble: 4 × 3
  Name     Age Salary
  <chr>  <dbl>  <dbl>
1 Johny     25  50000
2 Ali       30  60000
3 Boby      22  45000
4 Emilya    28  55000

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

Conclusion

Creating dataframes is a fundamental task in R for data analysis and manipulation. In this article, we discussed three methods to create dataframes: using the data.frame() function, reading data from external files using read.table() or read.csv() functions, and creating tibbles using the tibble package. Depending on your data and requirements, you can choose the method that best suits your needs.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads