【发布时间】:2021-08-06 12:00:48
【问题描述】:
我在 R 和 dplyr 中的数据方面需要帮助。 我的第一个问题在这里解决了:How can I mark (flag) first unique record as 1 and the rest similar records as 0 in data frame in R 但我需要改进这些数据。我使用如下代码:
df %>% mutate(drive = +!duplicated(paste(date, adress)))
结果如下:
jobs, date, adress, drive
1 111 28.03 bla 1
2 111 28.03 bla 0
3 111 28.03 bla 0
4 111 28.03 bla 0
5 111 28.03 bla 0
6 111 28.03 bla 0
7 111 28.03 bla 0
8 111 28.03 bla 0
9 111 28.03 bla 0 <- 9th record of the same job
10 111 28.03 bla 0 <- 10th record of the same job
11 345 05.03 bla 1
12 111 28.03 bla 0
13 236 28.03 abc 1
我需要改进一下我的 dplyr,我的数据应该是这样的:
jobs, date, adress, drive
1 111 28.03 bla 1
2 111 28.03 bla 0
3 111 28.03 bla 0
4 111 28.03 bla 0
5 111 28.03 bla 0
6 111 28.03 bla 0
7 111 28.03 bla 0
8 111 28.03 bla 0
9 111 28.03 bla 0 <- 9th record of the same job
10 111 28.03 bla 1 <- 10th record, it should be 1 not 0. Sum of "the same jobs" above 9 give me again flag 1.
11 345 05.03 bla 1 <- new record of the job, so 1
12 111 28.03 bla 0
13 236 28.03 abc 1
所以,第一个记录给我 1,同一份工作的第 2-9 条记录给我 0,同一工作的第 10 条记录给我再给我 1,第 11-19 条记录给我 0 等等。
【问题讨论】:
-
您的意思是第 1、第 10 和第 10 条记录的其他倍数要标记为 1?
-
是的,没错。记录 1、10 和 20 等。
-
但是您的样本数据不包含任何“工作”值?你的意思是
date改变是一份新工作吗? -
好的,我在上面编辑了一点我的数据。
-
是的,我的工作类似于工作/商家 ID。因此我们可以按工作 ID(可选地址)和日期进行分组。如果我们有其他日期,但工作 ID 相同,请重新开始计数。
标签: r duplicates unique dplyr