【问题标题】:Error in fix.by (by.x, x): 'by' must define one or more columns as numbers, names or logical datafix.by (by.x, x) 中的错误:“by”必须将一列或多列定义为数字、名称或逻辑数据
【发布时间】:2026-01-31 18:35:01
【问题描述】:

我有一些文件

file_analysis = read.xlsx(listfiles[1],sheetIndex = 1,header = FALSE)###
#View(file_analysis)
str(file_analysis)
file_comments =  read.csv("C:/Users/adm/Downloads/comments.csv",sep=";")
#View(file_comments)
file_groups = read.xlsx(listfiles[6],sheetIndex = 1,header = FALSE)  ####
#View(file_groups)
file_headeers = read.xlsx(listfiles[7],sheetIndex = 1,header = FALSE)
file_photos = read.csv("C:/Users/adm/Downloads/photos.csv",sep=";")
#View(file_photos)
file_profiles = read.xlsx(listfiles[12],sheetIndex = 1,header = FALSE) ####
#View(file_profiles)
file_profiles3 = read.xlsx(listfiles[13],sheetIndex = 1,header = FALSE)###
#View(file_profiles)
file_statistics = read.csv("C:/Users/adm/Downloads/statistics.csv",sep=";")
#View(file_statistics)
file_videos = read.csv("C:/Users/adm/Downloads/videos.csv",sep=";")
#View(file_videos)

我需要它合并到一个数据集 简单的方法

n=merge(file_comments,file_groups,file_photos ,file_profiles,
      file_profiles3,file_statistics,

      file_videos, by ="owner_id")

但它返回错误

Error in fix.by (by.x, x): 'by' must define one or more columns as numbers, names or logical data

这个 Error in fix.by(by.x, x) : 'by' must specify a uniquely valid columnmergedata <- merge (dataset1, dataset2, by.x="personalid") 和这个 Merging data - Error in fix.by(by.x, x) 是帮不了我。我不知道为什么。

owner_id 是数字

例子

258894746

3389571
3389572
3389573
3389574
118850

怎么了? 我需要一次加入所有文件。

【问题讨论】:

    标签: r merge na


    【解决方案1】:

    merge 不接受两个以上的数据帧。您应该使用Reducepurrr::reduce 函数see here 递归应用它

    基础 R

     Reduce(function(dtf1, dtf2) merge(dtf1, dtf2, by = "owner_id"),
               list(file_comments,file_groups,file_photos ,file_profiles,
                    file_profiles3,file_statistics,
                    file_videos)
            )
    

    tidyverse 语法

    library(dplyr)
    library(purrr)
    
    list(file_comments,file_groups,file_photos ,file_profiles,
          file_profiles3,file_statistics,
          file_videos) %>% reduce(inner_join, by = "owner_id")
    

    顺便说一句,如果您更喜欢左连接而不是内连接(您打算使用的那个):

    • merge 中添加all.x = TRUE 参数
    • 在 tidyverse 解决方案中使用 left_join 而不是 inner_join

    【讨论】:

      最近更新 更多