[R] attempted merge() returns: cannot coerce type 'closure' to vector of type 'any'
k.brand at erasmusmc.nl
Thu Dec 2 17:15:47 CET 2010
On 12/2/2010 3:52 PM, David Winsemius wrote:
> On Dec 2, 2010, at 5:58 AM, Karl Brand wrote:
>> On 12/2/2010 11:36 AM, David Winsemius wrote:
>>> On Dec 2, 2010, at 5:22 AM, Karl Brand wrote:
>>>> Cheers Bill.
> Inserting earlier debris:
>>>> I don't understand why i get this error message when attempting to
>>>> use merge() -
>>>> > temp <- merge(x, y[,17, drop=FALSE], by=rownames, sort=FALSE)
>>>> Error in as.vector(x, mode) :
>>>> cannot coerce type 'closure' to vector of type 'any'
>>> instead of
> At this point I think Bill should have suggested by="row.names" since
> that is the correct argument to merge() when asking for a rownames
> match, and you appear to have found that by experimentation since you
> didn't seem to have read the help page.
>>>> You got me halfway, since:
>>>> > temp <- merge(x=x, y=y[,17, drop=FALSE], by="rownames", sort=FALSE)
>>>> Error in fix.by(by.x, x) : 'by' must specify valid column(s)
>>>> but, using "row.names" instead of "rownames", like:
>>>> > temp <- merge(x=x, y=y[,17, drop=FALSE], by="row.names", sort=FALSE)
>>>> works (but adds a column "Row.names").
> OK. It added a column what's the problem? The help(merge) pages says:
> "If the matching involved row names, an extra character column called
> Row.names is added at the left, and in all cases the result has
> ‘automatic’ row names."
>>>> Which seems some what counter intuitive to me since i am feeding in
>>>> two matrices to the merge function, which i understand have
>>>> 'rownames', not 'row.names' as data frames have, right?
>>>> Although the
>>>> output of merge() is a data frame...
> Right. The merge function takes either dataframes or things which can be
> coerced to dataframes as arguments and returns a dataframe. That is
> exactly what the help(merge) page states outright (in many places). Just
> because your arguments were matrices doesn't mean the returned object
> should be one. Had you wanted it to be an "augmented" data.matrix with
> rownames as before, you could gotten that after merge by:
> temp2 <- datamatrix(temp[-1])
> rownames(temp2) <- temp$Row.names
> (Or by the rather nice manipulations that you described using rownames
> as indices.)
> I think I understand the "problem" now .... that you were expecting
> merge() to behave differently than is documented.
You nailed it- closer attention to the help page would defiently have
saved us time.
No less, thanks a lot for highlighting this, and also for the merge()
And insight into what physicists have to deaal with :)
Karl Brand <k.brand at erasmusmc.nl>
Department of Genetics
Dr Molewaterplein 50
3015 GE Rotterdam
P +31 (0)10 704 3211 | F +31 (0)10 704 4743 | M +31 (0)642 777 268
More information about the R-help