0

I am working in R and I have a Data set that has multiple entries for each subject. I want to create an index variable that indexes by subject. For example:

    Subject Index
1       A     1
2       A     2
3       B     1
4       C     1
5       C     2
6       C     3
7       D     1
8       D     2
9       E     1

The first A entry is indexed as 1, while the second A entry is indexed as 2. The first B entry is indexed as 1, etc.

Any help would be excellent!

Chal
  • 11
  • 2

2 Answers2

1

Here.s a quick data.table aproach

library(data.table)
setDT(df)[, Index := seq_len(.N), by = Subject][]
#    Subject Index
# 1:       A     1
# 2:       A     2
# 3:       B     1
# 4:       C     1
# 5:       C     2
# 6:       C     3
# 7:       D     1
# 8:       D     2
# 9:       E     1

Or with base R

with(df, ave(as.numeric(Subject), Subject, FUN = seq_along))
## [1] 1 2 1 1 2 3 1 2 1

Or with dplyr (don't run this on a data.table class)

library(dplyr)
df %>%
  group_by(Subject) %>%
  mutate(Index = row_number())
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
1

Using dplyr

library(dplyr)
df %>% group_by(Subject) %>% mutate(Index = 1:n())

You get:

#Source: local data frame [9 x 2]
#Groups: Subject
#
#  Subject Index
#1       A     1
#2       A     2
#3       B     1
#4       C     1
#5       C     2
#6       C     3
#7       D     1
#8       D     2
#9       E     1
Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77