gpt4 book ai didi

r - 结合两个数据框保留所有列

转载 作者:行者123 更新时间:2023-12-05 03:13:11 25 4
gpt4 key购买 nike

<分区>

我想做的是合并 2 个数据帧,保留所有列(在下面的示例中没有这样做)并在数据帧中存在来自不常见变量的间隙的地方输入零。

这似乎是一个 plyr 或 dplyr 主题。但是,plyr 中的完全连接不会保留所有列,而左连接或右连接不会保留我想要的所有行。查看 dplyr 备忘单(http://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf),一个 full_join 似乎是我需要的功能,但成功加载包后 R 无法识别此功能。

举个例子:

col1 <- c("ab","bc","cd","de")
col2 <- c(1,2,3,4)
df1 <- as.data.frame(cbind(col1,col2))
col1 <- c("ab","ef","fg","gh")
col3 <- c(5,6,7,8)
df2 <- as.data.frame(cbind(col1,col3))
library(plyr)
Example <- join(df1,df2,by = "col1", type = "full") #Does not keep col3
library(dplyr)
Example <- full_join(df1,df2,by = "col1") #Function not recognised

我想要输出...

col1 col2 col3
ab 1 5
bc 2 0
cd 3 0
de 4 0
ef 0 6
fg 0 7
gh 0 8

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com