Thomas
Thomas

Reputation: 1454

Create sequential number given two variables

I have a data.framethat looks similar to this one:

pp <- data.frame(a=c(1,1,1,2,2,3), zing=c("a", "b", "c", "d", "e", "f"))

pp

a   zing  
1   a     
1   b     
1   c     
2   d     
2   e     
3   f     

And I'd like to count the "uniqueness" of the variable zing given a. As a result, my data.frame should look like this:

a   zing  nr
1   a     1
1   b     2
1   c     3
2   d     1
2   e     2
3   f     1

Thanks for any help.

Upvotes: 1

Views: 81

Answers (4)

akrun
akrun

Reputation: 887881

Another option is to use getanID from splitstackshape

 library(splitstackshape)
 getanID(pp,'a')
 #   a zing .id
 #1: 1    a   1
 #2: 1    b   2
 #3: 1    c   3
 #4: 2    d   1
 #5: 2    e   2
 #6: 3    f   1

Upvotes: 4

talat
talat

Reputation: 70336

Here's another option using dplyr:

library(dplyr)
pp %>% group_by(a) %>% mutate(nr = row_number())

dplyr and data.table will both be more efficient than ave if you're working on large data sets. If the data is not that large, you might not need those packages.

Upvotes: 1

nrussell
nrussell

Reputation: 18612

An approach with data.table:

library(data.table)
##
q <- data.frame(
  a=c(1,1,1,2,2,3), 
  zing=c("a", "b", "c", "d", "e", "f"),
  stringsAsFactors=F)
setDT(q)
##
q[,nr:=1:.N,by=a]
##
> q
   a zing nr
1: 1    a  1
2: 1    b  2
3: 1    c  3
4: 2    d  1
5: 2    e  2
6: 3    f  1

Upvotes: 2

flodel
flodel

Reputation: 89097

q <- transform(q, nr = ave(a, a, FUN = seq_along))

Upvotes: 3

Related Questions