Ask Your Question
2

In R, what's the method for adding unique identifiers as columns when dealing with recurring rows in a data frame?

asked 2023-05-05 16:32:31 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-05 16:35:02 +0000

devzero gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-05 16:32:31 +0000

Seen: 9 times

Last updated: May 05 '23