Add Index ID to Data Frame in R (3 Examples)
In this article you’ll learn how to append an index ID to a data frame in R.
The article will consist of the following content:
So without further additions, let’s jump right to the programming part:
Creating Exemplifying Data
The following data is used as basement for this R tutorial:
data <- data.frame(x1 = 15:10, # Create example data x2 = letters[1:6], x3 = 4, row.names = LETTERS[16:21]) data # Print example data # x1 x2 x3 # P 15 a 4 # Q 14 b 4 # R 13 c 4 # S 12 d 4 # T 11 e 4 # U 10 f 4 |
data <- data.frame(x1 = 15:10, # Create example data x2 = letters[1:6], x3 = 4, row.names = LETTERS[16:21]) data # Print example data # x1 x2 x3 # P 15 a 4 # Q 14 b 4 # R 13 c 4 # S 12 d 4 # T 11 e 4 # U 10 f 4
As you can see based on the previous output of the RStudio console, our example data consists of six rows and three columns.
Our data frame does not contain an ID column. Furthermore, the row names of our data are some unordered alphabetical letters instead of a properly formatted index ID.
Example 1: Adding ID Column to Data Frame Using cbind & nrow Functions
This example shows how to append an index ID to our data frame using the cbind and nrow functions in R. Have a look at the following R code and its output:
data1 <- cbind(ID = 1:nrow(data), data) # Applying cbind function data1 # Printing updated data # ID x1 x2 x3 # P 1 15 a 4 # Q 2 14 b 4 # R 3 13 c 4 # S 4 12 d 4 # T 5 11 e 4 # U 6 10 f 4 |
data1 <- cbind(ID = 1:nrow(data), data) # Applying cbind function data1 # Printing updated data # ID x1 x2 x3 # P 1 15 a 4 # Q 2 14 b 4 # R 3 13 c 4 # S 4 12 d 4 # T 5 11 e 4 # U 6 10 f 4
As you can see, we have added a new ID variable at the first position of our data frame which ranges from 1 to 6.
Example 2: Changing Row Names to Index ID Using rownames & nrow Functions
This example shows how to use the row names of a data frame as IDs. For this, we can use the following R programming syntax:
data2 <- data # Replicating data rownames(data2) <- 1:nrow(data) # Applying rownames function data2 # Printing updated data # x1 x2 x3 # 1 15 a 4 # 2 14 b 4 # 3 13 c 4 # 4 12 d 4 # 5 11 e 4 # 6 10 f 4 |
data2 <- data # Replicating data rownames(data2) <- 1:nrow(data) # Applying rownames function data2 # Printing updated data # x1 x2 x3 # 1 15 a 4 # 2 14 b 4 # 3 13 c 4 # 4 12 d 4 # 5 11 e 4 # 6 10 f 4
The values within the data matrix were not changed, but the row names of our data were converted to a numeric range.
Example 3: Adding ID Column & Changing Row Names to Index Using dplyr Package
In this example, I’ll explain how to add an ID column AND how to modify the row names of our data frame using the dplyr package.
First, we need to install and load the dplyr package:
install.packages("dplyr") # Install & load dplyr library("dplyr") |
install.packages("dplyr") # Install & load dplyr library("dplyr")
Now, we can apply the mutate and row_number functions of the dplyr package as shown below:
data3 <- data %>% # Applying row_number function mutate(ID = row_number()) data3 # Printing updated data # x1 x2 x3 ID # 1 15 a 4 1 # 2 14 b 4 2 # 3 13 c 4 3 # 4 12 d 4 4 # 5 11 e 4 5 # 6 10 f 4 6 |
data3 <- data %>% # Applying row_number function mutate(ID = row_number()) data3 # Printing updated data # x1 x2 x3 ID # 1 15 a 4 1 # 2 14 b 4 2 # 3 13 c 4 3 # 4 12 d 4 4 # 5 11 e 4 5 # 6 10 f 4 6
The output is basically a combination of Examples 1 and 2.
Video, Further Resources & Summary
Do you need more info on the R programming code of this article? Then I can recommend having a look at the following video of my YouTube channel. In the video, I’m explaining the R code of this article:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you may have a look at the other tutorials that I have published on my website. A selection of articles on topics such as loops, naming data, indices, and variables can be found below:
- Get Column Index in Data Frame by Variable Name
- Get Row Index Number in R
- Append to Data Frame in Loop
- Add New Row at Specific Index Position to Data Frame
- R Programming Overview
In this tutorial, I showed how to create consecutive index IDs in R. Let me know in the comments section below, in case you have any further questions and/or comments.
Statistics Globe Newsletter