R 分组数据帧中组之间的比较
我试图在数据帧中的后续组中的项目之间执行比较-我想当您知道自己在做什么时,这是非常容易的 我的数据集可以表示如下:R 分组数据帧中组之间的比较,r,compare,dplyr,grouping,sequential,R,Compare,Dplyr,Grouping,Sequential,我试图在数据帧中的后续组中的项目之间执行比较-我想当您知道自己在做什么时,这是非常容易的 我的数据集可以表示如下: set.seed(1) data <- data.frame( date = c(rep('2015-02-01',15), rep('2015-02-02',16), rep('2015-02-03',15)), id = as.character(c(1005 + sample.int(10,15,replace=TRUE), 1005 + sample.int(10
set.seed(1)
data <- data.frame(
date = c(rep('2015-02-01',15), rep('2015-02-02',16), rep('2015-02-03',15)),
id = as.character(c(1005 + sample.int(10,15,replace=TRUE), 1005 + sample.int(10,16,replace=TRUE), 1005 + sample.int(10,15,replace=TRUE)))
)
然后我想按日期(groupby)对数据进行分组,然后在比较组之间的数据之前过滤掉重复的数据(distinct)。我想做的是每天确定哪些新id被添加,哪些id离开。因此,将第1天和第2天进行比较,以确定第2天中不在第1天的id和第1天中但在第2天中不存在的id,然后在第2天和第3天之间进行相同的比较,以此类推。使用anti_join(dplyr)可以很容易地进行比较,但我不知道如何引用数据集中的各个组 我的尝试(或其中一次尝试)如下所示:
date id
1/02/2015 1008
1/02/2015 1009
1/02/2015 1011
1/02/2015 1015
1/02/2015 1008
1/02/2015 1014
1/02/2015 1015
1/02/2015 1012
1/02/2015 1012
1/02/2015 1006
1/02/2015 1008
1/02/2015 1007
1/02/2015 1012
1/02/2015 1009
1/02/2015 1013
2/02/2015 1010
2/02/2015 1013
2/02/2015 1015
2/02/2015 1009
2/02/2015 1013
2/02/2015 1015
2/02/2015 1008
2/02/2015 1012
2/02/2015 1007
2/02/2015 1008
2/02/2015 1009
2/02/2015 1006
2/02/2015 1009
2/02/2015 1014
2/02/2015 1009
2/02/2015 1010
3/02/2015 1011
3/02/2015 1010
3/02/2015 1007
3/02/2015 1014
3/02/2015 1012
3/02/2015 1013
3/02/2015 1007
3/02/2015 1013
3/02/2015 1010
data %>%
group_by(date) %>%
distinct(id) %>%
do(lost = anti_join(., lag(.), by="id"))
date data
1 2015-02-01 list(id = c(3, 4, 6, 10, 9, 7, 1, 2, 8))
2 2015-02-02 list(id = c(5, 8, 10, 4, 3, 7, 2, 1, 9))
3 2015-02-03 list(id = c(6, 5, 2, 9, 7, 8))
4 2015-02-04 list(id = c(1, 5, 8, 7, 9, 3, 4, 6, 10))
5 2015-02-05 list(id = c(3, 5, 4, 7, 8, 1, 9))
但这当然不起作用,我只是得到:
Error in anti_join_impl(x, y, by$x, by$y) : Can't join on 'id' x 'id' because of incompatible types (factor / logical)
我试图做的是可能的,还是应该考虑编写一个笨拙的函数来完成它?只需将输入
stringsAsFactors=FALSE
添加到数据帧中即可。这将使您的代码运行:尽管我不确定输出的结果是否是您要查找的结果。要查看整个结果,请将其导入data.frame,然后查看它是否是您要查找的内容。希望这有帮助
set.seed(1)
data <- data.frame(
date = c(rep('2015-02-01',15), rep('2015-02-02',16), rep('2015-02-3',15)),
id = as.character(c(1005 + sample.int(10,15,replace=TRUE), 1005 + sample.int(10,16,replace=TRUE), 1005 + sample.int(10,15,replace=TRUE))),stringsAsFactors = FALSE)
data %>%
group_by(date) %>%
distinct(id) %>%
do(lost = anti_join(., lag(.), by="id"))%>%data.frame()
set.seed(1)
数据%
分组单位(日期)%>%
不同的(id)%>%
do(lost=anti_join(,lag(.),by=“id”))%>%data.frame()
对数据进行一些操作和合并可能会满足您的需要。像这样的
df <- unique(data)
df$date <- as.Date(df$date)
df$leftdate <- df$date + 1
df$prevdate <- df$date - 1
df2 <- cbind(df[,c("date","id")],flag = 1)
# merge the dataframe so that each day would attempt to join the next day
dfleft <- merge(df,df2,by.x = c("leftdate","id"),by.y = c("date","id"),all.x= TRUE)
# if there is no common id between a day and the next day, the merge returns NA, which is the desired results for those who left
dfleft <- dfleft[is.na(dfleft$flag),c("leftdate","id")]
# Here, you reverse the logic to find those who show up today but weren't there yesterday
dfnew <- merge(df,df2,by.x = c("prevdate","id"),by.y = c("date","id"),all.x= TRUE)
dfnew <- dfnew[is.na(dfnew$flag),c("date","id")]
df我对这个问题的理解是,数据显示了每个日期的id,我们希望遍历这些日期,将该日期的id与前一个日期的id进行比较
首先获取唯一的行u
,并将id
转换为数字。然后将id
按date
givings
进行拆分,并定义一个函数diff
,该函数使用负数为删除的id生成添加id的数字向量LAPPY
除了第一个组件外,其他组件都是按顺序排列的,因为它没有先前的组件。没有使用任何软件包
u <- unique(data)
u$id <- as.numeric(as.character(u$id))
s <- split(u$id, u$date)
diffs <- function(i) c(setdiff(s[[i]], s[[i-1]]), - setdiff(s[[i-1]], s[[i]]))
diffs_list <- setNames(lapply(seq_along(s)[-1], diffs), names(s)[-1])
或者如果您想要一个数据帧作为输出
setNames(stack(diffs_list), c("id", "date"))
给予:
> diffs_list
$`2015-02-02`
[1] 1010 -1011
$`2015-02-03`
[1] 1011 -1015 -1009 -1008 -1006
id date
1 1010 2015-02-02
2 -1011 2015-02-02
3 1011 2015-02-03
4 -1015 2015-02-03
5 -1009 2015-02-03
6 -1008 2015-02-03
7 -1006 2015-02-03
magrittr
这也可以使用magrittr包表示,如上面定义的diff
library(magrittr)
data %>%
unique %>%
transform(id = as.numeric(as.character(id))) %>%
{ split(.$id, .$date) } %>%
{ setNames(lapply(seq_along(.)[-1], diffs), names(.)[-1]) }
注意:我已将数据$date中的-3替换为-03。我肯定我不能投票支持我自己的答案,但我必须说我最喜欢我的答案。我希望得到一个使用dplyr工具来解决问题的答案,所以我一直在研究,我认为我现在有了一个(半)优雅的解决方案(除了我函数中的for循环)
以相同的方式生成示例数据集,但使用更多数据使其更有趣:
set.seed(1)
data <- data.frame(
date = c(rep('2015-02-01',15), rep('2015-02-02',16), rep('2015-02-03',15), rep('2015-02-04',15), rep('2015-02-05',15)),
id = as.character(c(1005 + sample.int(10,15,replace=TRUE), 1005 + sample.int(10,16,replace=TRUE), 1005 + sample.int(10,15,replace=TRUE), 1005 + sample.int(10,15,replace=TRUE), 1005 + sample.int(10,15,replace=TRUE)))
)
其中列表中的索引引用id的列表(奇怪但真实)
现在,我们可以通过索引编号引用组,即:
dataNested$data[[2]]
返回:
# A tibble: 9 × 1
id
<fctr>
1 1010
2 1013
3 1015
4 1009
5 1008
6 1012
7 1007
8 1006
#一个tible:9×1
身份证件
1 1010
2 1013
3 1015
4 1009
5 1008
6 1012
7 1007
8 1006
从这里开始,只需编写一个函数即可完成anti_join,让我们只知道后续各组之间的差异(尽管这是我并不感到自豪的部分,并且确实开始显示我缺乏R技能-请随时提出改进建议):
##函数defeed()-返回从每个后续时间段中删除的id
我怀疑你会发现一个预先制作的函数或一组函数会产生你想要的输出。我可能错了,但在(不同的)日期上循环似乎是不可避免的。对于每一个日期,你都会与前一个日期进行交叉和设置差异,以此类推。谢谢,你对03对3的看法是正确的。我已经在输入中更正了这一点。我将把这个问题再讨论一段时间,以收集更多的建议——谢谢大家。
# A tibble: 9 × 1
id
<fctr>
1 1010
2 1013
3 1015
4 1009
5 1008
6 1012
7 1007
8 1006
## Function departed() - returns the id's that were dropped from each subsequent time period
departed <- function(groups) {
tempList <- vector("list", nrow(groups))
# Loop through the groups and do an anti_join between each
for (i in seq(1, nrow(groups) - 1)) {
tempList[[i + 1]] <-
anti_join(data.frame(groups$data[[i]]), data.frame(groups$data[[i + 1]]), by = "id")
}
return(tempList)
}
> departedIDs <- dataNested %>% departed()
> departedIDs
[[1]]
NULL
[[2]]
id
1 1011
[[3]]
id
1 1006
2 1008
3 1009
4 1015
[[4]]
id
1 1007
[[5]]
id
1 1011
2 1015