[R] Newbie hung up with matrices

Chuck Cleland ccleland at optonline.net
Fri May 16 20:32:43 CEST 2003

rwatkins at cornerstonelp.com wrote:
> 	I just started learning R.  I'm trying to use the Help and the downloadable
> manuals.  I am stuck on trying to multiply matrices.  Can anyone please
> supply a couple of lines of code that I can plug into a fresh console to see
> how  a double precision (1x3) matrix is multiplied by a double precision
> (3x3) matrix?  I keep getting an error message,"Error in x%*%A: requires
> numeric matrix/vector arguments".

 > A <- matrix(c(1,4,7,2,5,8,3,6,9), ncol=3, byrow=TRUE)
 > x <- c(3,5,6)
 > x%*%A
      [,1] [,2] [,3]
[1,]   31   73  115

The result of read.csv is not a matrix, so try the following:

A <- as.matrix(A)

hope this helps,

Chuck Cleland

More information about the R-help mailing list