Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign order to elements in a column in R?

Tags:

r

In the following dataset:

Day   Place   Name
 22     X      A
 22     X      A
 22     X      B
 22     X      A
 22     Y      C
 22     Y      C
 22     Y      D
 23     X      B
 23     X      A

How can I assign numbering to the variable Name in following order using R:

Day   Place   Name  Number
 22     X      A     1
 22     X      A     1
 22     X      B     2
 22     X      A     1
 22     Y      C     1
 22     Y      C     1
 22     Y      D     2
 23     X      B     1
 23     X      A     2

In a nutshell, I need to number the names according to their order to occurrence on a certain day and at a certain place.

like image 265
Nadeem Hussain Avatar asked Oct 28 '14 11:10

Nadeem Hussain


1 Answers

In base R using tapply:

dat$Number <- 
unlist(tapply(dat$Name,paste(dat$Day,dat$Place),
       FUN=function(x){
         y <- as.character(x)
         as.integer(factor(y,levels=unique(y)))
}))

#    Day Place Name Number
# 1  22     X    A      1
# 2  22     X    A      1
# 3  22     X    B      2
# 4  22     Y    C      1
# 5  22     Y    C      1
# 6  22     Y    D      2
# 7  23     X    B      1
# 8  23     X    A      2

idea

  1. Group by Day and Place using tapply
  2. For each group, create a coerce the Name to the factor conserving the same order of levels.
  3. Coerce the created factor to integer to get the final result.

using data.table(sugar syntax) :

library(data.table)
setDT(dat)[,Number := {
  y <- as.character(Name)
  as.integer(factor(y,levels=unique(y)))
                   },"Day,Place"]

   Day Place Name Number
1:  22     X    A      1
2:  22     X    A      1
3:  22     X    B      2
4:  22     Y    C      1
5:  22     Y    C      1
6:  22     Y    D      2
7:  23     X    B      1
8:  23     X    A      2
like image 146
agstudy Avatar answered Sep 30 '22 20:09

agstudy