2

This is a similiar question to this but my output results are different.

Take the data:

example <- data.frame(var1 = c(2,3,3,2,4,5), 
                  var2 = c(2,3,5,4,2,5), 
                  var3 = c(3,3,4,3,4,5))

Now I want to create example$Identity which take a value from 1:x for each unique var1 value

I have used

example$Identity <- apply(example[,1], 2, function(x)(unique(x)))

But I am not familiar with correct formatting function()

The output of example$Identity should be 1,2,2,1,3,4

Community
  • 1
  • 1
lukeg
  • 1,327
  • 3
  • 10
  • 27
  • Do you need `1:n` groups based on `var1` only? Does this work for you: `as.numeric(as.factor(example$var1))`? – zx8754 May 19 '15 at 11:04
  • Yes, you answered just before the proposed answer, do you want to write and i will select and close the question – lukeg May 19 '15 at 11:07
  • 1
    I don't mind mark as answer Jaap's proposed answer. – zx8754 May 19 '15 at 11:08

2 Answers2

2

This:

example$Identity <- as.numeric(as.factor(example$var1))

will give you the desired result:

> example$Identity
[1] 1 2 2 1 3 4

By wrapping the as.factor in as.numeric it starts counting the factor levels with 1 and so on.

Jaap
  • 81,064
  • 34
  • 182
  • 193
2

Or you can use match

example$Identity <- with(example, match(var1, unique(var1)))

If the values are sorted as in the vector, findInterval can be also used

findInterval(example$var1, unique(example$var1))
#[1] 1 2 2 1 3 4
akrun
  • 874,273
  • 37
  • 540
  • 662