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

In the following dataset:

Day Place Name 22 XA 22 XA 22 XB 22 XA 22 YC 22 YC 22 YD 23 XB 23 XA 

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

 Day Place Name Number 22 XA 1 22 XA 1 22 XB 2 22 XA 1 22 YC 1 22 YC 1 22 YD 2 23 XB 1 23 XA 2 

In a nutshell, I need to specify the names in accordance with their order so that they appear on a specific day and in a certain place.

+5
source share
3 answers

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))) })) # 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 

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 
+3
source
 idx <- function(x) cumsum(c(TRUE, tail(x, -1) != head(x, -1))) transform(dat, Number = ave(idx(Name), Day, Place, FUN = idx)) # 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 
+1
source

Use ddply from plyr .

 dfr <- read.table(header = TRUE, text = "Day Place Name 22 XA 22 XA 22 XB 22 XA 22 YC 22 YC 22 YD 23 XB 23 XA") library(plyr) ddply( dfr, .(Day, Place), mutate, Number = as.integer(factor(Name, levels = unique(Name))) ) 

Or use dplyr , in the initneR answer option.

 library(dplyr) dfr %>% group_by(Day, Place) %>% mutate(Number = as.integer(factor(Name, levels = unique(Name)))) 
+1
source

Source: https://habr.com/ru/post/1205685/


All Articles