In the R database 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))) }))
Idea
- Grouping by day and place with
tapply - For each group, create a coercion of the Name to the coefficient, keeping the same level order.
- Link the created multiplier to an 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 XA 1 2: 22 XA 1 3: 22 XB 2 4: 22 YC 1 5: 22 YC 1 6: 22 YD 2 7: 23 XB 1 8: 23 XA 2
source share