Skip to content
Related Articles

Related Articles

Improve Article

Add Index ID to DataFrame in R

  • Last Updated : 29 Jul, 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




# declaring a data frame in 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)
 
# number of rows in data frame
num_rows = nrow(data_frame)
  
# creating ID column vector
ID <- c(1:num_rows)
 
# binding id column to the data frame
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




# declaring a data frame in 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)
 
# number of rows in data frame
num_rows = nrow(data_frame)
 
# changing row names of the 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




# declaring a data frame in 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)
 
# number of rows in data frame
num_rows = nrow(data_frame)
 
# creating ID column vector
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("Modifed 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] "Modifed 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



My Personal Notes arrow_drop_up
Recommended Articles
Page :