Level Up Your Data Wrangling: Adding Index Columns in R like a Pro!
code
rtip
operations
Author
Steven P. Sanderson II, MPH
Published
February 16, 2024
Introduction
Data wrangling in R is like cooking: you have your ingredients (data), and you use tools (functions) to prepare them (clean, transform) for analysis (consumption!). One essential tool is adding an “index column” – a unique identifier for each row. This might seem simple, but there are several ways to do it in base R and tidyverse packages like dplyr and tibble. Let’s explore and spice up your data wrangling skills!
Examples
Adding Heat with Base R
Ex 1: The Sequencer:
Imagine lining up your rows. cbind(df, 1:nrow(df)) adds a new column with numbers 1 to n, where n is the number of rows in your data frame (df).
# Sample datadf <-data.frame(name =c("Alice", "Bob", "Charlie"), age =c(25, 30, 28))# Add index using cbinddf_with_index <-cbind(index =1:nrow(df), df)df_with_index
index name age
1 1 Alice 25
2 2 Bob 30
3 3 Charlie 28
Ex 2: Row Name Shuffle:
Prefer names over numbers? rownames(df) <- 1:nrow(df) assigns row numbers as your index, replacing existing row names.