Fixing R Error – “cannot coerce type closure to vector of type character”

The r error “cannot coerce type closure to vector of type character” occurs when using the merge function and you try to add row names from an object. Fixing this problem simply involves removing such arguments do a merger or making the argument a character string. This problem is actually easier to fix than it is to understand.

Description of the error

The “cannot coerce type closure to vector of type character” error message occurs when using the merge function. This is a base r function that merges two or more data frames into a single data frame. You can use it to produce a wide array of data frames. Ironically, if you try using a character vector with this argument you will get another error message even though it is a vector of type character. However, you do not get an error message when using string variables. As a result, this supplies a workable way of fixing this problem. The key to understanding this problem is the fact that the “by” arguments require a character string to work.

Explanation of the error

Here we have a code example that triggers the “cannot coerce type closure to vector of type character” error message.

> x = as.data.frame(c(1, 2, 3, 4, 5, 6, 7, 8))
> y = as.data.frame(c(2, 3, 4, 5, 6, 7, 8, 9))
> colnames(y)[1] = ‘y’
> colnames(x)[1] = ‘x’
> merge(x,y, by.x=row.names, by.y=row.names)
Error in as.vector(x, mode) :
cannot coerce type ‘closure’ to vector of type ‘any’

In this example, we are using the merge function but the “by.x” and “by.y” arguments are not being given a string variable. Therefore, it produces our error message. It seems to arrive from confusion in the use of the row.names function because it does not produce either row or column names.

Examples of How to fix the R Error

Here are three examples of code that fixes the “cannot coerce type closure to vector of type character” Error message. These fixes are simple, but they do not produce the same results.

> x = as.data.frame(c(1, 2, 3, 4, 5, 6, 7, 8))
> y = as.data.frame(c(2, 3, 4, 5, 6, 7, 8, 9))
> colnames(y)[1] = ‘y’
> colnames(x)[1] = ‘x’
> merge(x,y)
x y
1 1 2
2 2 2
3 3 2
4 4 2
5 5 2
6 6 2
7 7 2
8 8 2
9 1 3
10 2 3
11 3 3
12 4 3
13 5 3
14 6 3
15 7 3
16 8 3
17 1 4
18 2 4

In this example, we simply remove the “by.x” and “by.y” arguments from the merge function. It fixes the problem, but it also produces a rather lengthy output, only part of which is shown here.

> x = as.data.frame(c(1, 2, 3, 4, 5, 6, 7, 8))
> y = as.data.frame(c(2, 3, 4, 5, 6, 7, 8, 9))
> colnames(y)[1] = ‘y’
> colnames(x)[1] = ‘x’
> merge(x,y, by.x=”x”, by.y=”y”)
x
1 2
2 3
3 4
4 5
5 6
6 7
7 8

In this example, we fix the problem by providing both of the “by.x” and “by.y” arguments in the merge function with a character, that corresponds to the column name in the data frame being referenced. It fixes the problem, but it does not produce a complete merger of the two data frames.

> x = as.data.frame(c(1, 2, 3, 4, 5, 6, 7, 8))
> y = as.data.frame(c(2, 3, 4, 5, 6, 7, 8, 9))
> colnames(y)[1] = ‘y’
> colnames(x)[1] = ‘x’
> merge(x,y, by.x=colnames(x), by.y=colnames(y))
x
1 2
2 3
3 4
4 5
5 6
6 7
7 8

In this example, we simply used “colnames” in place of “row.names.” This process fixes the problem and it produces the same result as the last example.

The “cannot coerce type closure to vector of type character” error message, is a problem that results from not properly using the “by” arguments in the merge function. Fortunately, it is an easy problem to fix once you understand it. You have two options for fixing this problem. You can omit the “by” arguments altogether or give them a string having the proper column name. They are simple fixes, but they do not produce the same results.

Scroll to top
Privacy Policy