根据日期和唯一标识分配数字

huangapple go评论64阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月9日 23:20:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定