How to transfer a matrix to r if the usual `t ()` does not work?

I have a matrix that I am trying to transpose in R, but the t () function does not return the correct answer. How to transfer the matrix?

> xx=matrix(c(3,7,4,8),2,byrow=TRUE) > xx [,1] [,2] [1,] 3 7 [2,] 4 8 > t(xx) [1] 0.7071068 0.7071068 
+7
source share
1 answer

This answer is incorrect, but in ways that were useful to me and may be for others, so I will leave it alone.

As @mnel noted, the underlying function R t() must be masked by another function with the same name. Try to remove the t() function and run t(xx) again. I guarantee that you will get the right results.

What do you get when you run this:

 rm(t) t(xx) 

If (despite my guarantee!) Still does not work, you can completely specify the version of t() that you want to use, for example:

 base::t(xx) 

That's why the two sentences above are not enough

From ?UseMethod :

Namespaces can register methods for common functions. To support this, 'UseMethod and' NextMethod look for methods in two places: first in the environment in which the generic function is called , and then in the registration database for the environment in which generic (usually this namespace). Therefore, methods for a generic function must be available in a generic call environment, or they must be registered. (It doesn’t matter if they are visible in the environment in which the generic type is defined.)

I mistakenly suggested that sending the S3 method is looking for methods such as t.default() first in base:::.__S3MethodsTable__. and then, possibly, in asNamespace("base") before searching in the calling environment, while the reverse side is closer to the truth.


Edit from GSee

Here's an interactive session to demonstrate what could be the problem.

 > t <- function(x, ...) print("generic masked") > t.default <- function(x, ...) print("t.default masked") > t.matrix <- function(x, ...) print("t.matrix was used") > t.numeric <- function(x, ...) print("t.numeric was used") > xx=matrix(c(3,7,4,8),2,byrow=TRUE) > t(xx) [1] "generic masked" > base::t(xx) [1] "t.matrix was used" > rm(t.matrix) > base::t(xx) [1] "t.numeric was used" > rm(t.numeric) > base::t(xx) [1] "t.default masked" > rm(t.default) > base::t(xx) [,1] [,2] [1,] 3 4 [2,] 7 8 
+12
source

All Articles