繁体   English   中英

r 合并命令返回列表,而不是数据框

[英]r merge command returning list, not data frame

昨天合并一直返回一个列表而不是一个数据框,将每一列作为列表中的一个单独元素分开。 当我尝试来自 plyr 的 join 命令时,它使除一个非按列连接之外的所有列在每一行中都具有完全相同的值。 今天合并命令起作用了,但我担心万一再次发生这种情况。 我不知道如何修复它。 有没有人听说过这个问题? 编辑:它不再工作了。

我正在尝试使用合并命令合并 2 个数据帧。 数据框是 tab2 和 tab 4,它们都有 4 个共同的列,我试图通过这些列进行合并。 Tab2 有一个额外的列 col5,而 tab4 有几个名为 sum1, ... sum7 的额外列。

这个命令:

temp <- merge(tab2, tab4, by = c("year", "month", "n_make", "n_mod"), all = TRUE, sort = FALSE)

产生错误:合并错误(tab2,tab4,by = c(“year”,“month”,“n_make”,“n_mod”),:未使用的参数(by = c(“year”,“month”,“ n_make", "n_mod"), all = TRUE, sort = FALSE)

当我删除“by”部分时,错误是一样的,只是提到了“all”和“sort”部分。 当我消除所有和排序部分时:

tmp <- merge(tab2, tab4)

命令通过了,但它没有给我一个数据框,而是给了我一个列表,其中第一个元素是 tab2 中的 col_5 列,它的元素数量与 tab2 的行数一样多。 其他条目以我的所有其他列命名,包括按列合并,并且它们只有与 tab4 一样多的条目。

我真的很感激人们可以就这个问题提供给我的任何信息。 我不能冒险再次发生。 我正在编写的代码必须与工作中的其他代码结合在一起,并且至少每月运行一次,所以我需要它一直工作。

这是 join 命令也不起作用:

library(plyr)
tmp <- join(tab2, tab4, type = "full")

编辑:可重现的例子由于数据是专有的,我重写了一个小例子,但它仍然不起作用。 我包含了我包含的包,以防其中一个把事情搞砸了:

library(RODBC)
library(RPostgres)              
library(DBI)
library(RPostgreSQL)
library(installr)
library(devtools)
library(remotes)
library(dbplyr)
library(dplyr)
# library(dbplot)   # not avaiable for R version 3.6.2
library(ggplot2)
library(modeldb)
library(tidypredict)
library(config)
library(inspectdf)
library(vcdExtra)
library(vcd)
library(janitor)
library(plyr)
library(openintro)
library(lattice)

year = c(2015, 2015, 2015, 2016, 2016, 2016, 2017, 2017, 2017, 2018, 2018, 2018, 2019, 2019, 2019)
month = c(1, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 3, 2, 2, 3)
n_make = c("A", "B", "A", "A", "A", "B", "A", "B", "C", "A", "B", "C", "B", "C", "C")
n_mod = c(7, 8, 7, 7, 9, 8, 7, 8, 6, 7, 8, 6, 8, 6, 5)
col5 = c(24, 38, 92, 41, 63, 19, 14, 8, 56, 73, 80, 13, 21, 42, 66)
t2 <-data.frame(year, month, n_make, n_mod, col5)
head(t2, 15)


year = c(2015, 2015, 2015, 2015, 2016, 2016, 2017, 2017, 2018, 2018, 2019, 2019, 2019, 2000)
month = c(1, 1, 1, 3, 1, 2, 2, 3, 1, 2, 1, 2, 2, 1)
n_make = c("A", "B", "C", "A", "A", "A", "B", "C", "A", "B", "B", "B", "C", "C")
n_mod = c(7, 8, 6, 7, 7, 9, 8, 6, 7, 8,  8, 8, 6, 5)
x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
y = c(14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
t4 <- data.frame(year, month, n_make, n_mod, x, y)
head(t4, 14)

# This is producing error messages.
t <- merge(t2, t4, by = c("year", "month", "n_make", "n_mod"), all = TRUE, sort = FALSE) 
t

#This is producing a regular list instead of a dataframe.
t <- merge(t2, t4)
t

我相信配置包掩盖了基本合并命令。 通过擦除所有输出并在注释掉该包的情况下重新启动 R,merge 命令现在可以工作了。

这很有趣,因为我尝试了 base::merge 以防万一屏蔽是问题,但这没有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM