Fixing R Errors – error in fix.by(by.y, y) : ‘by’ must specify a uniquely valid column

Despite being a part of programming, we will probably always find error messages annoying. After all, trying to fix them can take lots of time. This is particularly true when the error message does not give us any useful information about the problem. Unfortunately, the “error in fix.by(by.y, y) : ‘by’ must specify a uniquely valid column” error message falls under this category.

The circumstances of this error.

This error message occurs when you are merging two data frames together using the merge() function. Our error shows up in the particular case of merging the data frames around a particular column.

> a = rep(1, 5)
> b = rep(2, 5)
> c = rep(3, 5)
> d = c(“a”,”b”,”c”,”d”, “e”)
> df = data.frame(a,b,c)
> de = data.frame(a,b,d)
> merge(x = de, y = df)

It has in this example the data frames are not merged around a particular column, and you do not get an error message.

> a = rep(1, 5)
> b = rep(2, 5)
> c = rep(3, 5)
> d = c(“a”,”b”,”c”,”d”, “e”)
> df = data.frame(a,b,c)
> de = data.frame(a,b,d)
> merge(x = de, y = df, by = “d”)
Error in fix.by(by.y, y) : ‘by’ must specify a uniquely valid column

In the second example, the data frames are merged around a particular column our error message shows up. This shows that the problem is in that part of the code.

What is causing this error?

The cause of the error is merging around a column that is in only one of the data frames. The merge() function can only merge two data frames around a column that is in both.

> a = rep(1, 5)
> b = rep(2, 5)
> c = rep(3, 5)
> d = c(“a”,”b”,”c”,”d”, “e”)
> df = data.frame(a,b,c)
> de = data.frame(a,b,d)
> m1 = merge(x = de, y = df, by = “a”)
> m2 = merge(x = de, y = df, by = “d”)
Error in fix.by(by.y, y) : ‘by’ must specify a uniquely valid column

In this example, both data frames “m1” and “m2” are merged from data frames “de” and “df” but around different columns. “m1” is merged around column “a” which is in both data frames and the function does not produce an error message.”m2″ is merged around column”d” which is only in “de” resulting in our error message. This shows that the cause of the error message is merging around a column that is only in one of the two data frames.

How to fix this error.

Fixing this error is extremely easy. You just need to make sure the column you merge your data frames around is in both data frames.

> m1 = merge(x = de, y = df, by = “a”)
> m2 = merge(x = de, y = df, by = “d”)

This example shows the solution is simply a matter of changing the column name that the merger is occurring around. In this case, column “a” which is in both data frames replaces, column “d” which is in only one of the data frames.

Scroll to top
Privacy Policy