[R] as.matrix behaves weird

Charles C. Berry cberry at tajo.ucsd.edu
Sun Nov 14 19:36:13 CET 2010


On Sun, 14 Nov 2010, Feng Mai wrote:

> ncol is not an argument for as.matrix()
>

Perhaps this is more than Alexx wanted to know, but ...

Things can get a wee bit tricky when the function is a 'generic'.

More precisely:

'ncol' is not an argument for any method for as.matrix() that is on 
your search path.

as.matrix(x,...) is an S3 generic function. try

 	methods( as.matrix )

and you will see what methods are available for it. And some do have 
arguments other than 'x':

> args( as.matrix.data.frame )
function (x, rownames.force = NA, ...)
NULL
>

If you wanted to (but you don't, this is just didactic) you could make a 
method that accepts the ncol arg for a particular class

> as.matrix.columned <- function(x,ncol=1) matrix(x,ncol=ncol)
> x <- 1:3
> class(x) <- 'columned'
> as.matrix(x, ncol=3)
      [,1] [,2] [,3]
[1,]    1    2    3
>

HTH,

Chuck


> Alexx Hardt <mikrowelle1234 <at> gmx.de> writes:
>
>>
>> Hi,
>> can someone tell me why x is still a 2x1-matrix in the last step?
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Charles C. Berry                            Dept of Family/Preventive Medicine
cberry at tajo.ucsd.edu			    UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list