如何根据 R 数据集中的多列分配唯一编号?

How to assign a unique number based on multiple columns in R dataset?

我收集了一些数据,这些数据在年、月、级上都是独一无二的。我想单独为这三列的每一行分配一个唯一代码(简单数字)。关于如何进行的任何建议?

year <- c("A","J","J","S")
month <- c(2000,2001,2001,2000)
level <- c("high","low","low","low")
site <- c(1,2,3,3)
val1 <- c(1,2,3,0)

df <- data.frame(year,month,level,site,val1)

#Result desired
df$Unique.code --> 1,2,2,3

dplyr 具有 cur_group_id() 功能:

df %>%
  group_by(year, month, level) %>%
  mutate(id = cur_group_id())
# # A tibble: 4 × 6
# # Groups:   year, month, level [3]
#   year  month level  site  val1    id
#   <chr> <dbl> <chr> <dbl> <dbl> <int>
# 1 A      2000 high      1     1     1
# 2 J      2001 low       2     2     2
# 3 J      2001 low       3     3     2
# 4 S      2000 low       3     0     3

或者我们可以在 base:

中将 factor 强制转换为 integer
df$group_id = with(df, as.integer(factor(paste(year, month, level))))