[R] attempted merge() returns: cannot coerce type 'closure' to vector of type 'any'

David Winsemius dwinsemius at comcast.net
Thu Dec 2 15:52:58 CET 2010


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'
>>>
>> Try
>>    by="rownames"
>> instead of
>>    by=rownames

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:

?data.matrix
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.

-- 
David.


>> This reminds me of physicists using high energy particles to  
>> investigate
>> the structure of the nucleus. But you have alternatives to what  
>> might be
>> called "destructive debugging through binoculars". Instead of  
>> throwing
>> code at your data and asking the assembled audience of soothsayers to
>> tell you what went wrong by examining the debris, why don't you  
>> show us
>> what the data objects actually look like?
>>
>
> Ofcourse David, sorry- my lazy.
>
> Is str() enough to put away the binoculuars and maybe highlight the  
> source of my ignorance?
>
> > str(x)
> num [1:140, 1:7] 4.93e-02 2.83e-02 1.07e-02 2.68e-02 3.92e-05 ...
> - attr(*, "dimnames")=List of 2
>  ..$ : chr [1:140] "1415743_at" "1415887_at" "1416332_at"  
> "1416340_a_at" ...
>  ..$ : chr [1:7] "R.S.adj.pvalue" "R.S.Tau" "R.S.xpiek" "R.S.xdal" ...
>
> > str(y)
> num [1:140, 1:18] 0.573 1 0.752 0.768 0.399 ...
> - attr(*, "dimnames")=List of 2
>  ..$ : chr [1:140] "1427982_s_at" "1430520_at" "1454086_a_at"  
> "1419652_s_at" ...
>  ..$ : chr [1:18] "R.S.1" "R.S.2" "R.S.3" "R.S.4" ...
> > source(.trPaths[5], echo=TRUE, max.deparse.length=150)
>
> > str(temp)
> 'data.frame':   140 obs. of  9 variables:
> $ Row.names     :Class 'AsIs'  chr [1:140] "1415743_at" "1415887_at"  
> "1416332_at" "1416340_a_at" ...
> $ R.S.adj.pvalue: num  4.93e-02 2.83e-02 1.07e-02 2.68e-02  
> 3.92e-05 ...
> $ R.S.Tau       : num  21.6 23.6 26.6 29.7 18.8 20 24.6 27.9 23.9  
> 22.7 ...
> $ R.S.xpiek     : num  6.74 17.46 15.81 15.39 14.73 ...
> $ R.S.xdal      : num  16.94 1.76 22.8 1.12 5.41 ...
> $ R.S.p1        : num  0.0004 0.0001 0.0467 0.0024 0 ...
> $ R.S.p2        : num  0.0039 0 0.002 0.019 0.0001 0.0035 0.0351  
> 0.0126 0.0028 0.0192 ...
> $ R.S.p3        : num  0.0178 0.0004 0.0101 0.0312 0.0012 0.022  
> 0.0008 0.0151 0.0048 0.0317 ...
> $ V1            : num  3.96 -0.94 1.04 1.94 -2.53 ...
> >
>
> -- 
> Karl Brand <k.brand at erasmusmc.nl>
> Department of Genetics
> Erasmus MC
> Dr Molewaterplein 50
> 3015 GE Rotterdam
> P +31 (0)10 704 3211 | F +31 (0)10 704 4743 | M +31 (0)642 777 268

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list