1 | initial version |
One method for adding unique identifiers as columns when dealing with recurring rows in a data frame in R is to use the row_number()
function from the dplyr
package.
Here's an example:
library(dplyr)
# create example data frame with recurring rows
df <- data.frame(name = c("John", "Mary", "John", "Bob", "Mary"),
age = c(30, 25, 30, 40, 25))
# add unique identifier column
df <- df %>%
group_by(name, age) %>% # group by columns to identify recurring rows
mutate(id = row_number()) # use row_number() to add unique identifier column
df
This will output:
# A tibble: 5 x 3
# Groups: name, age [4]
name age id
<chr> <dbl> <int>
1 John 30 1
2 Mary 25 1
3 John 30 2
4 Bob 40 1
5 Mary 25 2
The id
column contains unique identifiers for each group of rows that have the same values in the name
and age
columns.