0

I would like to give a unique ID number in a data frame (df) based on certain conditions in the df. My df looks like this:

dfin <-
STUDY   DRUG  FED  DOSE  TIME  CONC
 4       0     1    50    2     10
 4       0     1    60    4     25
 5       1     1    10    5     20
 12      0     0    50    2     10

And so on. I want to add a unique ID number for each unique combination of STUDY, DRUG, FED, DOSE. The output for the above should be like this:

dfout <- 
STUDY   DRUG  FED  DOSE  TIME  CONC  ID
 4       0     1    50    2     10    1
 4       0     1    60    4     25    2
 5       1     1    10    5     20    3
 5       1     0    10    5     25    4  
 12      0     0    50    2     10    5

I would need help on how to do this in R.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Amer
  • 2,131
  • 3
  • 23
  • 38

2 Answers2

3

This is the point of the .GRP variable in data.table, see ?data.table:

.GRP is an integer, length 1, containing a simple group counter. 1 for the 1st group, 2 for the 2nd, etc.

Usage:

library(data.table)
setDT(dfin)[ , ID := .GRP, by = .(STUDY, DRUG, FED, DOSE)]

Also it's not clear you had data.table in mind when you tagged the question; if this is the case, see the Getting Started tutorials.

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • Thank you, that is really helpful. `data.table` was just a thought. I know that there are lots of things that can be done with it in data frame but I am still R beginners and didn't have clear vision on how I can possibly do it. – Amer Feb 23 '16 at 02:11
1

With dplyr, we can use group_indices_

library(dplyr)
dfin %>%
    mutate(ID= group_indices_(., 
        .dots= c("STUDY", "DRUG", "FED", "DOSE")))
#   STUDY DRUG FED DOSE TIME CONC ID
#1     4    0   1   50    2   10  1
#2     4    0   1   60    4   25  2
#3     5    1   1   10    5   20  3
#4    12    0   0   50    2   10  4
akrun
  • 874,273
  • 37
  • 540
  • 662