How to create, index and modify Data Frame in R?
Last Updated :
27 Jun, 2022
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 like an excel worksheet. In R, to create a Data Frame use data.frame() method. The syntax to create a data frame is given as-
data <- data.frame(columnName1=c(
data1,data2,...),
...........
columnNameN=c(data1,data2,...))
Example:
In this example let’s look into how to create a Data Frame in R using data.frame() method.
R
stats <- data.frame (player= c ( 'A' , 'B' , 'C' , 'D' ),
runs= c (100, 200, 408, NA ),
wickets= c (17, 20, NA , 5))
print ( "stats Dataframe" )
stats
|
Output
"stats Dataframe"
player runs wickets
1 A 100 17
2 B 200 20
3 C 408 NA
4 D NA 5
Indexing the Data Frame:
To access the particular data in the Data Frame use square brackets and specify the column name or row numbers, and column numbers to fetch. Let’s look into the syntaxes of different ways of indexing a data frame.
# fetching the data in particular column
data["columnName"]
# fetching data of specified rows and
# columns
data[ fromRow : toRow , columnNumber]
# fetches first row to third row
# and second column
Eg:- data[1:3,2]
Example:
In the below code we created a data frame and performed indexing on it by fetching the data in the specified rows and particular columns.
R
stats <- data.frame (player= c ( 'A' , 'B' , 'C' , 'D' ),
runs= c (100, 200, 408, NA ),
wickets= c (17, 20, NA , 5))
print ( "stats Dataframe" )
stats
stats[ "player" ]
print ( "----------" )
stats[1:3,2]
|
Output
"stats Dataframe"
player runs wickets
1 A 100 17
2 B 200 20
3 C 408 NA
4 D NA 5
----------
player
1 A
2 B
3 C
4 D
----------
100 200 408
Modify the Data Frame:
Data Modification in a Data Frame
To modify the data in a data frame, we use indexing and reassignment techniques. Let’s look into the syntax of how to modify the data in a data frame.
data[rowNumber, columnName] <- “newValue”
Adding a row to a Data Frame
To add a row in the data frame use rbind() function which accepts two parameters. One is a data frame and the other is the row we need to insert as a list of elements. The syntax of rbind is given below-
rbind( dataframeName, list( data1, data2, …))
Adding a column to a Data Frame
To add a column to a data frame use cbind() function which accepts two parameters. One is a data frame to which we add a new column and the other is data in the new column with the column name. Below is the syntax of cbind() function.
cbind( dataframeName, columnName = c(data1, data2, …))
Removing a row and column from a Data Frame
To remove a row and column from a data frame using the below syntax
# remove row from a dataframe
# deletes the row of specified row number
dataframeName <- dataframeName[-rowNumber,]
# remove column from a dataframe
dataframeName$columnName <- NULL
Example:
In the example, we created a data frame and performed modification operations like insertion, deletion, and modification on the Dataframe.
R
stats <- data.frame (player= c ( 'A' , 'B' , 'C' , 'D' ),
runs= c (100, 200, 408, NA ),
wickets= c (17, 20, NA , 5))
cat ( "stats Dataframe\n" )
stats
stats[4, "runs" ] <- 274
cat ( "\nModified dataframe\n" )
stats
cat ( "\nDataFrame after a row insertion\n" )
stats<- rbind (stats, list ( 'E' ,500,1))
print (stats)
cat ( "\nDataFrame after a new column insertion\n" )
stats<- cbind (stats,matches= c (2,3,10,2,12))
print (stats)
stats<-stats[-2,]
stats$wickets<- NULL
cat ( "\nDataframe after deletion of a row & column\n" )
stats
|
Output
stats Dataframe
player runs wickets
1 A 100 17
2 B 200 20
3 C 408 NA
4 D NA 5
Modified dataframe
player runs wickets
1 A 100 17
2 B 200 20
3 C 408 NA
4 D 274 5
DataFrame after a row insertion
player runs wickets
1 A 100 17
2 B 200 20
3 C 408 NA
4 D 274 5
5 E 500 1
DataFrame after a new column insertion
player runs wickets matches
1 A 100 17 2
2 B 200 20 3
3 C 408 NA 10
4 D 274 5 2
5 E 500 1 12
Dataframe after deletion of a row & column
player runs matches
1 A 100 2
3 C 408 10
4 D 274 2
5 E 500 12
Similar Reads
How to create dataframe in R
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
3 min read
How to Address Error in as.data.frame in R
R Programming Language is widely used for data analysis and visualization. The as. data.frame() function is frequently employed to convert different types of objects, such as matrices, lists, or factors, into data frames. However, users may encounter errors during this conversion process. In this ar
2 min read
Create Matrix and Data Frame from Lists in R Programming
In R programming, there 5 basic objects. Lists are the objects that can contain heterogeneous types of elements, unlike vectors. Matrices can contain the same type of elements or homogeneous elements. On the other hand, data frames are similar to matrices but have an advantage over matrices to keep
3 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
How to find length of data frame in R
In this article, we will see What is a Data Frame and how to find the length of a data frame in R programming Language. Return the length (total number of rows) of the Data Frame using nrow()nrow() function is used to return the number of rows of the specified object (Matrix/DataFrame etc). we will
3 min read
How to convert excel content into DataFrame 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 convert excel content into DataFrame in R Programming. To read an excel file itself, read.xlsx() function from xlsx is used. Installation This module
2 min read
Create a dataframe in R with different number of rows
In this article, we will explore how to create a data frame with different numbers of rows by using the R Programming Language. How do we create the data frame?data. frame() is a function that is used to create the data frame. By using these functions provided by R, it is possible to create the data
2 min read
Indexing and Slicing Data Frames in R
In this article let's discuss indexing and slicing the Data Frames or how to access elements of a data frame in R Programming Language. What is Indexing or accessing?The process of accessing particular data components or subsets within a vector, matrix, or data frame is called indexing. It enables u
4 min read
How to Create a DataFrame with Nested Array
DataFrames are the most fundamental structures for managing and modifying data in the R Programming Language. They exhibit data in two dimensions, in rows and columns, with each column containing a distinct type of data. While traditional DataFrames are good at handling fundamental data types like n
3 min read
How to plot a subset of a dataframe in R ?
In this article, we will learn multiple approaches to plotting a subset of a Dataframe in R Programming Language. Here we will be using, R language's inbuilt "USArrests" dataset. Method 1: Using subset() function In this method, first a subset of the data is created base don some condition, and then
2 min read