Create an individual variable conditionally
A simple solution using the data.table
package might be:
library(data.table)df$Indiv <- as.integer(df$Indiv)setDT(df)[, Indiv := .GRP, by = .(Time, Server, Subject)]df# Date Time Period Server Subject Indiv# 1: 1004 9 1 1 1 1# 2: 1004 9 1 1 2 2# 3: 1004 9 1 2 1 3# 4: 1004 9 1 2 2 4# 5: 1004 9 2 1 1 1# 6: 1004 9 2 1 2 2# 7: 1004 9 2 2 1 3# 8: 1004 9 2 2 2 4# 9: 1004 13 1 1 1 5#10: 1004 13 1 1 2 6#11: 1004 13 1 2 1 7#12: 1004 13 1 2 2 8#13: 1004 13 2 1 1 5#14: 1004 13 2 1 2 6#15: 1004 13 2 2 1 7#16: 1004 13 2 2 2 8
Or if you prefer dplyr
:
library(dplyr)df$Indiv <- df %>% group_indices(Time, Server, Subject)