How to create dataframe in R
Last Updated :
18 Apr, 2024
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.
Similar Reads
Create table from DataFrame in R In this article, we are going to discuss how to create a table from the given Data-Frame in the R Programming language. Function Used: table(): This function is an essential function for performing interactive data analyses. As it simply creates tabular results of categorical variables. Syntax: tabl
3 min read
How to create, index and modify Data Frame in R? In this article, we will discuss how to create a Data frame, index, and modify the data frame in the R programming language. Creating a Data Frame:A Data Frame is a two-dimensional labeled data structure. It may consist of fields/columns of different types. It simply looks like a table in SQL or lik
4 min read
How to Delete DataFrames in R? In R, a DataFrame is a data structure which can be two-dimensional, that is it can be used to hold data in rows and columns. To create a DataFrame, you can use the data.frame() function. but after you're done with a DataFrame, you may wish to remove it so that memory can be released or your workspac
3 min read
How to Export DataFrame to CSV in R ? R Programming language allows us to read and write data into various files like CSV, Excel, XML, etc. In this article, we are going to discuss how to Export DataFrame to CSV file in R Programming Language. Approach:Â Write Data in column wise formatCreate DataFrame for these dataWrite Data to the CS
1 min read
How to Convert a List to a Dataframe in R We have a list of values and if we want to Convert a List to a Dataframe within it, we can use a as.data.frame. it Convert a List to a Dataframe for each value. A DataFrame is a two-dimensional tabular data structure that can store different types of data. Various functions and packages, such as dat
4 min read