I have a grouped dataset, but the groups don\'t have a unique identifier, like this:
direction <- c(\'N\',\'S\',\'W\',\'N\',\'N\',\'S\',\'W\',\'N\',\'S\',\'W\
I think this should work:
direction <- c('N','S','W','N','N','S','W','N','S','W')
measurement <- c(4,6,1,7,2,4,7,4,1,4)
x <- data.frame(direction, measurement)
inds <- which(direction == 'N')
diffs <- diff(c(inds, length(direction)+1))
groups <- rep(seq_along(inds),diffs)
x$ID <- LETTERS[groups]
x
Note that if you have more than 26 groups, LETTERS[]
will start returning NA. You can always just use:
x$ID <- groups
If that happens.