在R中交叉加入dplyr [英] Cross Join in dplyr in R
本文介绍了在R中交叉加入dplyr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
library(dplyr)
cust_time<-data.frame(cid=c("c1","c2","c3","c4","c5"),ts=c(2,7,11,13,17))
#I want to do a cross join on self, preferable in dplyr else base package is Ok
#But w/o renaming header names
#Currently I have to create a duplicate cust_time to do this.
cust_time.1<-rename(cust_time,cid1=cid,ts1=ts)
merge(cust_time,cust_time.1,by=NULL)
#Later I will want to do cross join within the grouped region
cust_time <-mutate(cust_time,ts.bucket=ts%/%10)
#If using duplicate tables, not sure, how to do the below
#group_by(cust_time,ts.bucket) %>%
#do cross join within this bucket
基本上,我想在表上进行交叉自连接,但是由于我无法使用dplyr解决方案,因此我使用了基本包。但这需要我重命名所有列。但是,我后来希望能够在分组级别进行交叉联接,这就是我绊脚石的地方。
任何帮助表示赞赏。
Basically, I want to do a cross self-join on a table but since I cant a dplyr solution, I used the base package. But it requires me to rename all the columns. However, I later want to be able do a cross-join at a grouped level and this is where I am stumbling.
Any help appreciated.
推荐答案
您只需要一个虚拟列即可加入:
You just need a dummy column to join on:
cust_time$k <- 1
cust_time %>%
inner_join(cust_time, by='k') %>%
select(-k)
或者如果您不想修改原始数据框:
Or if you don't want to modify your original dataframe:
cust_time %>%
mutate(k = 1) %>%
replicate(2, ., simplify=FALSE) %>%
Reduce(function(a, b) inner_join(a, b, by='k'), .) %>%
select(-k)
这篇关于在R中交叉加入dplyr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文