Add Index ID to DataFrame in R - GeeksforGeeks

13 Aug, 2021
In this article, we will discuss how index ID can be added to dataframes in the R programming language.
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 Rdata_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 framenum_rows = nrow(data_frame)  # creating ID column vectorID <- c(1:num_rows) # binding id column to the data framedata_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
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 Rdata_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 framenum_rows = nrow(data_frame) # changing row names of the data framerownames(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
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 Rdata_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 framenum_rows = nrow(data_frame) # creating ID column vectordata_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
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
surindertarika1234
sweetyty
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
Data Visualization in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
Logistic Regression in R Programming
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Merge DataFrames by Column Names in R