Add a unique identifier to the same column value in R data frame

sophia

I have a data frame as follows:

     index val  sample_id
1     1    14      5
2     2    22      6
3     3    1       6
4     4    25      7
5     5    3       7
6     6    34      7

For each row with the sample_id, I would like to add a unique identifier as follows:

index val  sample_id
1     1    14      5
2     2    22      6-A
3     3    1       6-B
4     4    25      7-A
5     5    3       7-B
6     6    34      7-C

Any suggestion? Thank you for your help.

LMc

Using dplyr:

library(dplyr)

dplyr::group_by(df, sample_id) %>% 
  dplyr::mutate(sample_id = paste(sample_id, LETTERS[seq_along(sample_id)], sep = "-"))

 index   val sample_id
  <int> <dbl> <chr>    
1     1    14 5-A      
2     2    22 6-A      
3     3     1 6-B      
4     4    25 7-A      
5     5     3 7-B      
6     6    34 7-C 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

in R how to apply a value of a column to multiple columns in the same data frame

R - number of unique values in a column of data frame

Add value from one data frame into another data frame in R

Add column to aggregate from data frame in R

filtering entire data frame based on a unique value and create a new column in R

How to filter a column in a data frame by the regex value of another column in same data frame in Pyspark

add rows to data frame for each value from a character column R

Create data.frame based on unique column values in R?

Update duplicate data with same unique-identifier

create a smaller data frame from unique value from a column

using a row value as an identifier of values in the same column (Data wrangling)

Using a variable to add a data frame column in R

R: Calculating offset differences between elements in data frame with the same identifier

Aggregate a data frame per unique value of a column

How to add data from column in a data frame to a corpus based on a value from another column in R?

How to add value to column when data frame is empty in r

Convert data frame of values into binary data frame where each unique value is a column

Create additional rows in data frame in R for unique elements in column

Transpose data frame variables and add null, unique counts in [r]

r - Replace values in a data.frame column with a different value in the same column based unique ID

Aggregate by group AND add column to data frame in R

How to add 1) add unique identifier every 3 rows of data frame

Add new column in pyspark data frame comparing two column present in same data frame

Aggregate data frame based on column value in R

Subtract multiple column in the same data frame in R

Creating multiple copies of the same data frame with unique values in a new column

Add together values of unique column combinations in data frame

how to stack the same data frame in itself and change a value in a column per stack in r

Add new columns to data frame for each unique value in another column