英文:
Assign number based on date and unique identifier
问题
日期 | 标签 | 捕获顺序 |
---|---|---|
1/21/22 | 001 | 1 |
1/22/22 | 001 | 2 |
1/23/22 | 001 | 3 |
1/21/22 | 002 | 1 |
1/22/22 | 002 | 2 |
1/23/22 | 002 | 3 |
英文:
I have a dataframe that look like this:
Date | Tag |
---|---|
1/21/22 | 001 |
1/22/22 | 001 |
1/23/22 | 001 |
1/21/22 | 002 |
1/22/22 | 002 |
1/23/22 | 002 |
The entire dataframe is grouped by tag number and then sorted by date oldest to newest. I want to create a new column that assigns 1 for first capture 2 for second capture etc. and should look like this.
Date | Tag | CaptureOrder |
---|---|---|
1/21/22 | 001 | 1 |
1/22/22 | 001 | 2 |
1/23/22 | 001 | 3 |
1/21/22 | 002 | 1 |
1/22/22 | 002 | 2 |
1/23/22 | 002 | 3 |
答案1
得分: 2
base R 使用 seq_along
按组进行操作:
transform(df, CaptureOrder = ave(Tag, Tag, FUN = seq_along))
Date Tag CaptureOrder
1 1/21/22 1 1
2 1/22/22 1 2
3 1/23/22 1 3
4 1/21/22 2 1
5 1/22/22 2 2
6 1/23/22 2 3
data.table,使用 rowid
:
library(data.table)
setDT(df)[, CaptureOrder := rowid(Tag)]
dplyr,使用 row_number
:
library(dplyr)
mutate(df, CaptureOrder = row_number(), .by = Tag)
英文:
base R with seq_along
by group:
transform(df, CaptureOrder = ave(Tag, Tag, FUN = seq_along))
Date Tag CaptureOrder
1 1/21/22 1 1
2 1/22/22 1 2
3 1/23/22 1 3
4 1/21/22 2 1
5 1/22/22 2 2
6 1/23/22 2 3
data.table, with rowid
:
library(data.table)
setDT(df)[, CaptureOrder := rowid(Tag)]
dplyr, with row_number
:
library(dplyr)
mutate(df, CaptureOrder = row_number(), .by = Tag)
答案2
得分: 2
你可以在包dplyr
的版本 >= 1.1.0 中使用consecutive_id
函数,在Tag
列上分组。
library(dplyr)
df %>% mutate(CaptureOrder = consecutive_id(Date), .by = Tag)
Date Tag CaptureOrder
1 1/21/22 1 1
2 1/22/22 1 2
3 1/23/22 1 3
4 1/21/22 2 1
5 1/22/22 2 2
6 1/23/22 2 3
英文:
You can use the consecutive_id
function from the package dplyr
version >= 1.1.0 while have the group on the Tag
column.
library(dplyr)
df %>% mutate(CaptureOrder = consecutive_id(Date), .by = Tag)
Date Tag CaptureOrder
1 1/21/22 1 1
2 1/22/22 1 2
3 1/23/22 1 3
4 1/21/22 2 1
5 1/22/22 2 2
6 1/23/22 2 3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论