简体   繁体   English

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

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

I collected some data that is different in unique in year, month, and level.我收集了一些数据,这些数据在年、月、级别上都是不同的。 I want to assign a unique code (simple numerics) to each row on these three columns alone.我想单独为这三列的每一行分配一个唯一代码(简单数字)。 Any suggestions on how to proceed?关于如何进行的任何建议?

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 has the cur_group_id() function for this: dplyrcur_group_id() function:

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

Or we could coerce a factor into an integer in base:或者我们可以将一个factor强制转换为基数中的integer

df$group_id = with(df, as.integer(factor(paste(year, month, level))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM