Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge problems in R: Error in fix.by(by.x, x) : 'by' must specify uniquely valid columns

Tags:

merge

r

I have two tables, both of which are aggregate outputs. I'd like to merge them together - they were both aggregated off the same data, so they have the same column names, but for the sake of edification I thought I'd try the specific by.x and by.y methods in the examples of ?merge.

merge(medA,countA, 
      by = c("manager_id", "manager_name", "grouping"))
## this works

merge(medA,countA, 
  by.x = c(medA$manager_id, medA$manager_name, medA$grouping), 
  by.y = c(countA$manager_id, countA$manager_name, countA$grouping))
## this doesn't? 
## Error in fix.by(by.x, x) : 'by' must specify uniquely valid columns

It isn't particularly pressing: I can just use the merge that works. But I'm honestly confused why specific by.x and by.y doesn't work. I've googled around for it, but I can't seem to find similar problems. Any thoughts you guys have would be much appreciated.

like image 444
yjtan Avatar asked Oct 09 '15 21:10

yjtan


People also ask

How does merge work in R?

Merge data frames in R. The R merge function allows merging two data frames by common columns or by row names. This function allows you to perform different database (SQL) joins, like left join, inner join, right join or full join, among others.

How do I merge two Dataframes in R?

In R we use merge() function to merge two dataframes in R. This function is present inside join() function of dplyr package. The most important condition for joining two dataframes is that the column type should be the same on which the merging happens. merge() function works similarly like join in DBMS.

How do I merge columns in R?

How do I concatenate two columns in R? To concatenate two columns you can use the <code>paste()</code> function. For example, if you want to combine the two columns A and B in the dataframe df you can use the following code: <code>df['AB'] <- paste(df$A, df$B)</code>.

How do I combine three data frames in R?

Join Multiple R DataFrames To join more than two (multiple) R dataframes, then reduce() is used. It is available in the tidyverse package which will convert all the dataframes to a list and join the dataframes based on the column.


1 Answers

Before using merge() also see if the classes of two tables you are trying to merge are same. If you try to merge data.table class with data.frame in that case also,it will throw this error.

like image 134
Sai Prabhanjan Reddy Avatar answered Sep 18 '22 13:09

Sai Prabhanjan Reddy