是否存在用于与all = TRUE合并的R dplyr方法? [英] Is there an R dplyr method for merge with all=TRUE?
问题描述
我有两个要合并的R数据帧.在R中,您可以执行以下操作:
成本<-data.frame(农场= c('农场A','办公室'),成本= c(10,100))树木<-data.frame(农场= c('农场A','农场B'),树木= c(20,30))合并(成本,树木,全部=真)
产生:
农场成本树1个农场A 10 202个办事处100 NA3农场B NA 30
我正在使用 dplyr
,并且希望使用以下解决方案:
left_join(成本,树木)
产生的东西接近我想要的东西:
农场成本树1个农场A 10 202个办事处100 NA
在 dplyr
中,我可以看到 left_join
, inner_join
, semi_join
和 anti-join
,但是这些都不是与 all = TRUE
合并的合并
.
还-是否有一种将NA设置为0的快速方法?到目前为止,我使用 x $ trees [is.na(x $ trees)]<-0;
的工作很费力(我需要每列一个命令),而且似乎并不总是有效./p>
谢谢
dplyr
的最新版本(0.4.0)现在具有full_join选项,这是我相信您想要的./p>
成本<-data.frame(农场= c('农场A','办公室'),成本= c(10,100))树木<-data.frame(农场= c('农场A','农场B'),树木= c(20,30))合并(成本,树木,全部=真)
返回
>合并(成本,树木,全部=真)农场成本树1个农场A 10 202个办事处100 NA3农场B NA 30
还有
库(dplyr)full_join(成本,树木)
返回
>full_join(成本,树木)加入方式:农场"农场成本树1个农场A 10 202个办事处100 NA3农场B NA 30警告信息:不同水平的连接因子,强制到特征向量
I have two R dataframes I want to merge. In straight R you can do:
cost <- data.frame(farm=c('farm A', 'office'), cost=c(10, 100))
trees <- data.frame(farm=c('farm A', 'farm B'), trees=c(20,30))
merge(cost, trees, all=TRUE)
which produces:
farm cost trees
1 farm A 10 20
2 office 100 NA
3 farm B NA 30
I am using dplyr
, and would prefer a solution such as:
left_join(cost, trees)
which produces something close to what I want:
farm cost trees
1 farm A 10 20
2 office 100 NA
In dplyr
I can see left_join
, inner_join
, semi_join
and anti-join
, but none of these does what merge
with all=TRUE
does.
Also - is there a quick way to set the NAs to 0? My efforts so far using x$trees[is.na(x$trees)] <- 0;
are laborious (I need a command per column) and don't always seem to work.
thanks
The most recent version of dplyr
(0.4.0) now has a full_join option, which is what I believe you want.
cost <- data.frame(farm=c('farm A', 'office'), cost=c(10, 100))
trees <- data.frame(farm=c('farm A', 'farm B'), trees=c(20,30))
merge(cost, trees, all=TRUE)
Returns
> merge(cost, trees, all=TRUE)
farm cost trees
1 farm A 10 20
2 office 100 NA
3 farm B NA 30
And
library(dplyr)
full_join(cost, trees)
Returns
> full_join(cost, trees)
Joining by: "farm"
farm cost trees
1 farm A 10 20
2 office 100 NA
3 farm B NA 30
Warning message:
joining factors with different levels, coercing to character vector
这篇关于是否存在用于与all = TRUE合并的R dplyr方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!