3

I have some files

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)

i need it merge to one dataset simple way

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

      file_videos, by ="owner_id")

but it returns me error

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

this Error in fix.by(by.x, x) : 'by' must specify a uniquely valid columnmergedata <- merge (dataset1, dataset2, by.x="personalid") and this Merging data - Error in fix.by(by.x, x) is not help me. And i don't know why.

owner_id is numeric

example

258894746

3389571
3389572
3389573
3389574
118850

What's wrong? i need join all files at once.

psysky
  • 3,037
  • 5
  • 28
  • 64

1 Answers1

3

merge does not accept more than two dataframes. You should apply it recursively with Reduce or purrr::reduce function see here

Base 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 syntax

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")

By the way, if you prefer left join rather than inner join (the one you intended to use):

  • add all.x = TRUE argument in merge
  • use left_join rather than inner_join in tidyverse solution
linog
  • 5,786
  • 3
  • 14
  • 28