2

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

2 Answers 2

4

One option could be:

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

[1] "a" "a" "a" "b" "b" "c" "c" "c" "c" "d"
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.