我正在使用 R 并且我有一个如下所示的数据框:
df<-data.frame(
tree_id=c("t1","t1","t1","t1","t1","t1","t1","t1","t1","t2","t2","t2","t2","t2","t2","t2","t2","t2"),
branch_id=c("b1","b1","b1","b1","b1","b1","b1","b3","b3","b1","b1","b1","b1","b2","b2","b2","b2","b2"),
bud_id_rank=c("1","2","4","7","9","12","15","1","3","1","2","5","9","1","5","7","8","12")
)
我想添加一个名为“new_rank”的新列,它基于
branch_id 和 tree_id 从 1 开始升序。结果应该是这样的:
df<-data.frame(
tree_id=c("t1","t1","t1","t1","t1","t1","t1","t1","t1","t2","t2","t2","t2","t2","t2","t2","t2","t2"),
branch_id=c("b1","b1","b1","b1","b1","b1","b1","b3","b3","b1","b1","b1","b1","b2","b2","b2","b2","b2"),
bud_id_rank=c("1","2","4","7","9","12","15","1","3","1","2","5","9","1","5","7","8","12"),
new_rank=c("1","2","3","4","5","6","7","1","2","1","2","3","4","1","2","3","4","5")
)
是否有任何功能可以快速使用例如 plyr 包?
提前致谢