[英]r merge command returning list, not data frame
Yesterday merge kept returning a list instead of a data frame, separating out each column as a separate element in the list.昨天合并一直返回一个列表而不是一个数据框,将每一列作为列表中的一个单独元素分开。 When I tried the join command from plyr, it was making all but one of my non-join by columns take on the exact same value in every row.
当我尝试来自 plyr 的 join 命令时,它使除一个非按列连接之外的所有列在每一行中都具有完全相同的值。 Today the merge command worked, but I'm worried in case this ever happens again.
今天合并命令起作用了,但我担心万一再次发生这种情况。 I don't know how to fix it.
我不知道如何修复它。 Has anyone ever heard of this problem?
有没有人听说过这个问题? EDIT: It is no longer working again.
编辑:它不再工作了。
I am trying to merge 2 data frames using the merge command.我正在尝试使用合并命令合并 2 个数据帧。 The data frames are tab2 and tab 4 and they both have 4 columns in common by which I am trying to merge.
数据框是 tab2 和 tab 4,它们都有 4 个共同的列,我试图通过这些列进行合并。 Tab2 has one extra columns, col5, while tab4 has several extra columns named sum1, ... sum7.
Tab2 有一个额外的列 col5,而 tab4 有几个名为 sum1, ... sum7 的额外列。
This command:这个命令:
temp <- merge(tab2, tab4, by = c("year", "month", "n_make", "n_mod"), all = TRUE, sort = FALSE)
produced an error: Error in merge(tab2, tab4, by = c("year", "month", "n_make", "n_mod"), : unused arguments (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)
When I eliminated the "by" part, the error was the same, just mentioning the "all" and "sort" parts.当我删除“by”部分时,错误是一样的,只是提到了“all”和“sort”部分。 When I eliminated the all and sort parts:
当我消除所有和排序部分时:
tmp <- merge(tab2, tab4)
the command went through, but instead of giving me a data frame, it gave me a list, where the first element was the col_5 column from tab2 and it had as many elements as tab2 has rows.命令通过了,但它没有给我一个数据框,而是给了我一个列表,其中第一个元素是 tab2 中的 col_5 列,它的元素数量与 tab2 的行数一样多。 The other entries were named after all my other columns, including the merge by columns, and they only had as many entries as tab4.
其他条目以我的所有其他列命名,包括按列合并,并且它们只有与 tab4 一样多的条目。
I would really appreciate ANY information people can give me on this problem.我真的很感激人们可以就这个问题提供给我的任何信息。 I cannot risk it happening again.
我不能冒险再次发生。 The code I am writing has to be combined with other code at work and will be run on at least a monthly basis, so I need it to work all the time.
我正在编写的代码必须与工作中的其他代码结合在一起,并且至少每月运行一次,所以我需要它一直工作。
Here is the join command which did not work either:这是 join 命令也不起作用:
library(plyr)
tmp <- join(tab2, tab4, type = "full")
EDIT: reproducible example Since the data is proprietary, I rewrote a small example and it still doesn't work.编辑:可重现的例子由于数据是专有的,我重写了一个小例子,但它仍然不起作用。 I'm including packages I included in case one of them is messing things up:
我包含了我包含的包,以防其中一个把事情搞砸了:
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
I believe the config package is masking the base merge command.我相信配置包掩盖了基本合并命令。 By erasing all output and restarting R with that package commented out, the merge command now works.
通过擦除所有输出并在注释掉该包的情况下重新启动 R,merge 命令现在可以工作了。
It's funny, because I tried base::merge just in case a masking was the problem, but that didn't work.这很有趣,因为我尝试了 base::merge 以防万一屏蔽是问题,但这没有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.