1

I know using match() will extract the first occurrence of a variable, but how can I create a new column labeling the first occurrence?

for example how could I get:

example <- data.frame(id = c(1,1,1,2,3,4), label = c(1,0,0,1,1,1))

thanks in advance!

Henrik
  • 65,555
  • 14
  • 143
  • 159
vizidea
  • 153
  • 7

2 Answers2

4

An option with duplicated in base R

example$label <- +(!duplicated(example$id))

-output

example
#  id label
#1  1     1
#2  1     0
#3  1     0
#4  2     1
#5  3     1
#6  4     1

duplicated returns a logical column with TRUE for duplicate elements of 'id' and FALSE for non-duplicates i.e. the first occurrence. Negating (!) reverses the TRUE -> FALSE and viceversa, then coerce it to binary with + or as.integer

akrun
  • 874,273
  • 37
  • 540
  • 662
2

Another base R option using ´ave`

transform(
  example,
  label_new = +(ave(id, id, FUN = seq_along) == 1)
)

gives

  id label label_new
1  1     1         1
2  1     0         0
3  1     0         0
4  2     1         1
5  3     1         1
6  4     1         1
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81