[英]Assign running number based on row entries R
Probably something very easy for you guys.对你们来说可能很容易。 As indicated in the title, I would like to create a new column having running numbers based on row entries from a different column (in this case ASV column).如标题所示,我想根据来自不同列的行条目(在本例中为 ASV 列)创建一个具有运行编号的新列。 So the row entries in reference column has duplicate values.因此参考列中的行条目具有重复值。
ASV New_column
wthjjwjjgbwurigwe434j 1
wthjjwjjgbwurigwe434j 1
wthjjwjjgbwurigwe434j 1
21y4hghgw6yw8ngqoigj7 2
21y4hghgw6yw8ngqoigj7 2
1387341yqfysddhas394h 3
Appreciate your help.感谢你的帮助。
If we assume your data frame is named 'dat' we can use the following code:如果我们假设您的数据框名为“dat”,我们可以使用以下代码:
dat$New_column <- as.integer(factor(dat$ASV))
Updated I decided to come up with another solution as rleid
may lead to misleading result.更新我决定提出另一种解决方案,因为rleid
可能会导致误导性结果。
library(dplyr)
df %>%
mutate(dup = +duplicated(df$ASV),
id = cumsum(dup == 0)) %>%
select(-dup)
ASV id
1 wthjjwjjgbwurigwe434j 1
2 wthjjwjjgbwurigwe434j 1
3 wthjjwjjgbwurigwe434j 1
4 21y4hghgw6yw8ngqoigj7 2
5 21y4hghgw6yw8ngqoigj7 2
6 1387341yqfysddhas394h 3
We could use match
我们可以使用match
dat$New_column <- with(data, match(ASV, unique(ASV)))
If new ids are to be allocated alphabetically, dense_rank
in dplyr
can be used如果要按字母顺序分配新的 id,可以使用dense_rank
中的dplyr
df %>% mutate(New_column = dense_rank(ASV))
ASV New_column
1 wthjjwjjgbwurigwe434j 3
2 wthjjwjjgbwurigwe434j 3
3 wthjjwjjgbwurigwe434j 3
4 21y4hghgw6yw8ngqoigj7 2
5 21y4hghgw6yw8ngqoigj7 2
6 1387341yqfysddhas394h 1
OR或者
df %>% group_by(ASV) %>%
mutate(New_column = cur_group_id())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.