我们可以用group_indices
from dplyr
library(dplyr)
df %>%
mutate(style_serial = sample(6000)[group_indices(.,fact_code, style_, buyer)])
# fact_code style_ item buyer style_serial
#1 1206 -23 LADIES TANK TOP 652 5778
#2 1206 -23 LADIES TANK TOP 652 5778
#3 1206 -23 LADIES TANK TOP 652 5778
#4 1214 593935_592435 SS T-SHIRT 254 998
#5 1214 593935_592435 SS T-SHIRT 254 998
#6 1214 593935_592435 SS T-SHIRT 254 998
#7 7022 1572472 T-SHIRT 338 3018
#8 7022 1572472 T-SHIRT 338 3018
#9 7022 1572472 T-SHIRT 338 3018
注意:数字是random
化为sample
,如果我们不需要它,则删除sample
part
df %>%
mutate(style_serial = group_indices(.,fact_code, style_, buyer))
或者使用base R
v1 <- with(df, do.call(paste, df[1:3]))
df$style_serial <- match(v1, unique(v1))
data
df <- structure(list(fact_code = c(1206L, 1206L, 1206L, 1214L, 1214L,
1214L, 7022L, 7022L, 7022L), style_ = c("-23", "-23", "-23",
"593935_592435", "593935_592435", "593935_592435", "1572472",
"1572472", "1572472"), item = c("LADIES TANK TOP", "LADIES TANK TOP",
"LADIES TANK TOP", "SS T-SHIRT", "SS T-SHIRT", "SS T-SHIRT",
"T-SHIRT", "T-SHIRT", "T-SHIRT"), buyer = c(652L, 652L, 652L,
254L, 254L, 254L, 338L, 338L, 338L)), class = "data.frame", row.names = c(NA,
-9L))