jose
jose

Reputation: 103

R: grouping factors everytime n unique levels occur in data

I want a new grouping every time there's 5 unique levels

Ex. varB is the desired result

structure(list(varA = structure(c(2L, 2L, 3L, 5L, 4L, 1L, 1L, 
2L, 3L, 5L, 4L, 4L, 1L), .Label = c("badger", "cat", "dog", "monkey", 
"turtle"), class = "factor"), varB = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L)), .Names = c("varA", "varB"), class = "data.frame", row.names = c(NA, 
-13L))

edit: some assumptions about the data:

Upvotes: 4

Views: 243

Answers (1)

IRTFM
IRTFM

Reputation: 263481

Using modulo division on cumsum of "new" values:

dat$cu5 <- with(dat, 1+ cumsum( c(0, varA[-length(varA)] != varA[-1])) %/% 5)

Adding one is only needed if you want the numbering to start at 1. If you factored it and added labels it would not be needed.

Upvotes: 4

Related Questions