Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign a unique ID number to each group of identical values in a column [duplicate]

I have a data frame with a number of columns. I would like to create a new column called “id” that gives a unique id number to each group of identical values in the “sample” column.

Example data:

# dput(df) df <- structure(list(index = 1:30, val = c(14L, 22L, 1L, 25L, 3L, 34L,  35L, 36L, 24L, 35L, 33L, 31L, 30L, 30L, 29L, 28L, 26L, 12L, 41L,  36L, 32L, 37L, 56L, 34L, 23L, 24L, 28L, 22L, 10L, 19L), sample = c(5L,  6L, 6L, 7L, 7L, 7L, 8L, 9L, 10L, 11L, 11L, 12L, 13L, 14L, 14L,  15L, 15L, 15L, 16L, 17L, 18L, 18L, 19L, 19L, 19L, 20L, 21L, 22L,  23L, 23L)), .Names = c("index", "val", "sample"), class = "data.frame",  row.names = c(NA, -30L))  head(df)   index val sample  1     1  14      5   2     2  22      6   3     3   1      6   4     4  25      7   5     5   3      7   6     6  34      7   

What I would like to end up with:

  index val sample id 1     1  14      5  1 2     2  22      6  2 3     3   1      6  2 4     4  25      7  3 5     5   3      7  3 6     6  34      7  3 
like image 684
jjulip Avatar asked Jun 09 '14 11:06

jjulip


People also ask

How do you add unique ID numbers to duplicate rows in Excel?

1. type 1 into the cell which is adjacent to the first data you want to add ID number. 2. Then in the cell below it, type this formula =IF(B1=B2,A1,A1+1), press Enter key to get the first result, drag fill handle down until last data showing up.

How do I duplicate unique values in Excel?

To filter for unique values, click Data > Sort & Filter > Advanced. To remove duplicate values, click Data > Data Tools > Remove Duplicates. To highlight unique or duplicate values, use the Conditional Formatting command in the Style group on the Home tab.

How do I create a unique ID in Excel?

Observe the cell now has the formula that generates a unique ID and a unique ID value. Copy (Ctrl+C) the same first cell of the range ( A2 ). Paste (Ctrl+V) into all cells in the range ( A2:A10 ). Observe each cell now has the formula that generates a unique ID and its own unique ID value.


1 Answers

How about

df2 <- transform(df,id=as.numeric(factor(sample))) 

?

I think this (cribbed from Add ID column by group) should be slightly more efficient, although perhaps a little harder to remember:

df3 <- transform(df, id=match(sample, unique(sample))) all.equal(df2,df3)  ## TRUE 

If you want to do this in tidyverse:

library(dplyr) df %>% group_by(sample) %>% mutate(id=cur_group_id()) 
like image 139
Ben Bolker Avatar answered Sep 22 '22 02:09

Ben Bolker