-1

I am trying to group data based on uniqueLocation using R.

it starts at 101 and goes to 701.

I want 101 to be 1 and 701 to be 4

This would go up to a max of 16, once it hits 16 in the count it would reset back to 1 and continue to 16 until the data set has been assigned a value between 1 & 16

Crazyness
  • 15
  • 4
  • Welcome to SO! Can you please share a reproducible example of your data? You can use the `dput` function on a subset of your dataset. Preferably with the counter-reseting cases. – Lstat May 13 '20 at 08:22

1 Answers1

0

You could use match and unique to get a unique ID number for each entry. You can then use %% 16 to reset it after every 16 value.

df$ID <- match(df$UniqueLocation, unique(df$UniqueLocation)) %% 16
df$ID[df$ID == 0] <- 16

To assign a unique string for each 16 values we can use :

df$ID1 <- paste0('Test_', ceiling(match(df$UniqueLocation, 
                                  unique(df$UniqueLocation))/16))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • this worked, thanks dude. So if I was to allocate each group of a 16 its own unique string e.g. for the first set of 16 = Test_1, second set of 16 Test_2 how would I do that? – Crazyness May 13 '20 at 09:18