Add Index ID to DataFrame in R
Last Updated :
13 Aug, 2021
In this article, we will discuss how index ID can be added to dataframes in the R programming language.
Method 1 : Using cbind() and nrow() methods
The nrow() method in R Programming language is used to compute the number of rows in the dataframe that is specified as an argument of this method. cbind() method in R language is used to append a vector to the dataframe. The vector is appended to the dataframe in the order in which it is specified during the function call. In order, to lead the dataframe with an id vector, the following syntax will be used:
cbind(vec , data_frame)
The vector length should be equivalent to the number of rows in the data frame.
Example:
R
data_frame <- data.frame (x1 = 2:7,
x2 = letters [1:6],
x3 = 6,
row.names = c ( 'I' , 'II' , 'III' , 'IV' , 'V' , 'VI' ))
print ( "Original Data Frame" )
print (data_frame)
num_rows = nrow (data_frame)
ID <- c (1:num_rows)
data_frame1 <- cbind (ID , data_frame)
print ( "Modified Data Frame" )
print (data_frame1)
|
Output
[1] "Original Data Frame"
x1 x2 x3
I 2 a 6
II 3 b 6
III 4 c 6
IV 5 d 6
V 6 e 6
VI 7 f 6
[1] "Modified Data Frame"
ID x1 x2 x3
I 1 2 a 6
II 2 3 b 6
III 3 4 c 6
IV 4 5 d 6
V 5 6 e 6
VI 6 7 f 6
Method 2: Assigning row names as the index ID in the dataframe
In order to lead a dataframe with the index ID column, we can also reassign the row names of the dataframe to reflect the increasing integer values starting from 1 to the number of rows in the data frame. The rownames(df) method is used to assign the row names. All the changes are reflected in the original dataframe.
Example:
R
data_frame <- data.frame (x1 = 2:7,
x2 = letters [1:6],
x3 = 6,
row.names = c ( 'I' , 'II' , 'III' , 'IV' , 'V' , 'VI' )
)
print ( "Original Data Frame" )
print (data_frame)
num_rows = nrow (data_frame)
rownames (data_frame) <- c (1:num_rows)
print ( "Modified Data Frame" )
print (data_frame)
|
Output
[1] "Original Data Frame"
x1 x2 x3
I 2 a 6
II 3 b 6
III 4 c 6
IV 5 d 6
V 6 e 6
VI 7 f 6
[1] "Modified Data Frame"
x1 x2 x3
1 2 a 6
2 3 b 6
3 4 c 6
4 5 d 6
5 6 e 6
6 7 f 6
Method 3 : Using seq.int() method
seq.int() method in R is used to generate integer sequences beginning from 1 to the number x specified as an argument of the function. The row names have pertained. The newly added column is appended at the end of the data frame.
Syntax:
seq.int(x)
Example:
R
data_frame <- data.frame (x1 = 2:7,
x2 = letters [1:6],
x3 = 6,
row.names = c ( 'I' , 'II' , 'III' , 'IV' , 'V' , 'VI' )
)
print ( "Original Data Frame" )
print (data_frame)
num_rows = nrow (data_frame)
data_frame$ID <- seq.int (num_rows)
print ( "Modified Data Frame" )
print (data_frame)
|
Output
[1] "Original Data Frame"
x1 x2 x3
I 2 a 6
II 3 b 6
III 4 c 6
IV 5 d 6
V 6 e 6
VI 7 f 6
[1] "Modified Data Frame"
x1 x2 x3 ID
I 2 a 6 1
II 3 b 6 2
III 4 c 6 3
IV 5 d 6 4
V 6 e 6 5
VI 7 f 6 6
Method 4: Using dplyr library
The mutate method of the dplyr package can be used to add, remove and modify more data to the included data frame object. In order to add a new column, the following variant of mutating method can be used :
Syntax:
mutate(new-col-name = logic)
where the logic specifies the condition upon which data addition is based upon
Here, the row_number() method is used to provide an increasing sequence of integers to store row numbers. The newly added column is appended at the end of the existing data object.
Example:
R
library (dplyr)
data_frame <- data.frame (x1 = 2:7,
x2 = letters [1:6],
x3 = 6
)
print ( "Original Data Frame" )
print (data_frame)
data_frame <- data_frame %>% mutate (ID = row_number ())
print ( "Modified Data Frame" )
print (data_frame)
|
Output
[1] "Original Data Frame"
x1 x2 x3
1 2 a 6
2 3 b 6
3 4 c 6
4 5 d 6
5 6 e 6
6 7 f 6
[1] "Modified Data Frame"
x1 x2 x3 ID
1 2 a 6 1
2 3 b 6 2
3 4 c 6 3
4 5 d 6 4
5 6 e 6 5
6 7 f 6 6
Similar Reads
How to add Header to Dataframe in R ?
A header necessarily stores the names or headings for each of the columns. It basically helps the user to identify the role of the respective column in the data frame. The top row containing column names is called the header row of the data frame. In this article, we will learn how to add a Header t
3 min read
How to add dataframe to dataframe in R ?
In this article, we will see how to add dataframe at the end of another dataframe in R Programming Language. Method 1: Using rbind() method The rbind() method in R works only if both the input dataframe contains the same columns with similar lengths and names. The dataframes may have a different num
4 min read
How to add column to dataframe in R ?
In this article, we are going to see how to add columns to dataframe in R. First, let's create a sample dataframe. Adding Column to the DataFrame We can add a column to a data frame using $ symbol. syntax: dataframe_name $ column_name = c( value 1,value 2 . . . , value n) Here c() function is a vect
2 min read
Convert Matrix to Dataframe in R
In this article, we will discuss how to convert the matrix into DataFrame, or we can also say that we will discuss how to create a DataFrame from a matrix in R Programming Language. A matrix can be converted to a dataframe by using a function called as.data.frame(). It will take each column from the
1 min read
How to Select DataFrame Columns by Index in R?
In this article, we will discuss how to select columns by index from a dataframe in R programming language. Note: The indexing of the columns in the R programming language always starts from 1. Method 1: Select Specific Columns By Index with Base R Here, we are going to select columns by using index
2 min read
How to add a row to R dataframe ?
In this article, we will see how to add rows to a DataFrame in R Programming Language. To do this we will use rbind() function. This function in R Language is used to combine specified Vector, Matrix or Data Frame by rows. Syntax: rbind(dataframe 1, dataframe 2) Example 1 : C/C++ Code # creating a d
2 min read
How to insert blank row into dataframe in R ?
In this article, we will discuss how to insert blank rows in dataframe in R Programming Language. Method 1 : Using nrow() method The nrow() method in R is used to return the number of rows in a dataframe. A new row can be inserted at the end of the dataframe using the indexing technique. The new row
3 min read
Insert multiple rows in R DataFrame
In this article, we are going to see how to insert multiple rows in the dataframe in R Programming Language. First, let's create a DataFrame To create a data frame we need to use vectors. We need to create vectors with some values and pass the vectors into data.frame() function as parameter. Thus, a
4 min read
How to change Row Names of DataFrame in R ?
The rows are stacked together, each denoted by a unique name. By default, the integer identifiers beginning from 1 to the number of rows are assigned to the data frame by default. The task here is to change the Rows names in given dataframe using R programming. Dataset in use: First SecondThird1a72a
3 min read
How To Merge Two DataFrames in R ?
In this article, We are going to see how to merge two R dataFrames. Merging of Data frames in R can be done in two ways. Merging columnsMerging rowsMerging columns In this way, we merge the database horizontally. We use the merge function to merge two frames by one or more common key variables(i.e.,
2 min read