-1

I have a data frame (df):

a <- c("up","up","up","up","down","down","down","down")
b <- c("l","r","l","r","l","l","r","r")


df <- data.frame(a,b)

I would like to add a third column (c) which contains the order of entries, grouped by columns a and b that looks something like this:

   a   b c
1   up l 1
2   up r 1
3   up l 2
4   up r 2
5 down l 1
6 down l 2
7 down r 1
8 down r 2

I have tried solutions using dplyr that have not worked:

order <- df %>%
   group_by(a) %>%
   group_by(b) %>%
   mutate(c = row_number()) # This counts the order based on `b`, ignoring `a`

order <- df %>%
       group_by(a) %>%
       group_by(b) %>%
       mutate(c = seq_len(n())) # This counts the order based on `b`, ignoring `a`    

I would prefer to keep using dplyr and pipes if possible, but other suggestions are welcome

John L. Godlee
  • 559
  • 5
  • 21

1 Answers1

3

You need to combine a and b in the same group_by statement.

order <- df %>%
  group_by(a, b) %>%
  mutate(c = row_number()) 

order

# Source: local data frame [8 x 3]
# Groups: a, b [4]
# 
#       a      b     c
#   <fctr> <fctr> <int>
# 1     up      l     1
# 2     up      r     1
# 3     up      l     2
# 4     up      r     2
# 5   down      l     1
# 6   down      l     2
# 7   down      r     1
# 8   down      r     2
MeetMrMet
  • 1,349
  • 8
  • 14