maycca
maycca

Reputation: 4080

R create group variable based on row order and condition

I have a dataframe containing multiple groups that are not explicitly stated. Instead, new group always start when type == 1, and is the same for following rows, containing type == 2. The number of rows per group can vary.

How can I explicitly create new variable based on order of another column? The groups, of course, should be exclusive.

My data:

df <- data.frame(type = c(1,2,2,1,2,1,2,2,2,1),
                 stand = 1:10)

Expected output with new group myGroup:

   type stand myGroup
1     1     1       a
2     2     2       a
3     2     3       a
4     1     4       b
5     2     5       b
6     1     6       c
7     2     7       c
8     2     8       c
9     2     9       c
10    1    10       d

Upvotes: 2

Views: 445

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101044

Here is another option using rep() + diff(), but not as simple as the approach by @tmfmnk

idx <- which(df$type==1)
v <- diff(which(df$type==1))
df$myGroup <- rep(letters[seq(idx)],c(v <- diff(which(df$type==1)),nrow(df)-sum(v)))

such that

> df
   type stand myGroup
1     1     1       a
2     2     2       a
3     2     3       a
4     1     4       b
5     2     5       b
6     1     6       c
7     2     7       c
8     2     8       c
9     2     9       c
10    1    10       d

Upvotes: 0

tmfmnk
tmfmnk

Reputation: 39858

One option could be:

with(df, letters[cumsum(type == 1)])

[1] "a" "a" "a" "b" "b" "c" "c" "c" "c" "d"

Upvotes: 4

Related Questions