作为R新手,我想基于现有的变量生成一个新的id变量:
df = read.table(text =
"id
10
10
15
15
19
34
", sep = "", header = TRUE)
新ID应从1开始,如下所示:
df = read.table(text =
"id new_id
10 1
10 1
15 2
15 2
19 3
34 4
", sep = "", header = TRUE)
我怎样才能做到这一点?
答案 0 :(得分:3)
试试这个:
transform(df, new_id=as.numeric(factor(id)))