我正在尝试编写一段代码来检查列中的日期是否相同。该行将按ID分组,因此,如果组中的任何日期与ID不同,则会对其进行标记。
这是我的数据帧(代码):
df <- structure(list(ID = c("P40", "P40", "P40", "P40", "P42", "P42"),
Date = dmy(c(26072013, 26072013, 2092012, 23082012, 01072014, 01072014))),
class = "data.frame", row.names = c(NA, -6L))表格形式:
ID Date
P40 2013-07-26
P40 2013-07-26
P40 2012-09-24
P40 2012-08-23
P42 2014-07-01
P42 2014-07-01 这就是我想要实现的:
ID Contsistent
P40 No
P42 Yes发布于 2019-07-26 23:36:29
一种选择是
library(dplyr)
df %>%
group_by(ID) %>%
summarise(Consistent = factor(n_distinct(Date) == 1,
levels = c(FALSE, TRUE), labels = c("No", "Yes")))
# A tibble: 2 x 2
# ID Consistent
# <chr> <fct>
#1 P40 No
#2 P42 Yes 或使用data.table
library(data.table)
setDT(df)[, .(Consistent = uniqueN(Date) == 1), .(ID)]
# ID Consistent
#1: P40 FALSE
#2: P42 TRUE发布于 2019-07-26 23:36:22
也许:
library(dplyr)
df %>%
group_by(ID) %>%
summarise(Consistent = c("No", "Yes")[(n_distinct(Date) == 1) + 1])输出:
# A tibble: 2 x 2
ID Consistent
<chr> <chr>
1 P40 No
2 P42 Yes 发布于 2019-07-26 23:47:17
我们可以使用R base中的聚合来完成此操作
# Check Date for each ID in df
aggregate(Date ~ ID, data= df, function(ID_i){
# if there is only one unique Date value in ID_i then the function gives "Yes", otherwise it gives a "No"
ifelse(length(unique(ID_i)) == 1, "Yes", "No")})
ID Date
1 P40 No
2 P42 Yeshttps://stackoverflow.com/questions/57223111
复制相似问题