[R] how to return multipy matrix in a function

zhaoxing731 zhaoxing731 at yahoo.com.cn
Mon Feb 7 18:30:33 CET 2011


Hello

 I have a 100*100 matrix which is from a intensive computation, e.g. mat. Is there any method/function that return the max of every row and the subscript of maximum value simultaneously

#define the function
> returnfunction<-function(x){
+ value<-apply(x,1,max)
+ index<-apply(x,1,which.max)
+ }


> mat<-matrix(c(3,5,7,2,1,10,4,3,2),3)#initilize the matrix for test
> mat
     [,1] [,2] [,3]
[1,]    3    2    4
[2,]    5    1    3
[3,]    7   10    2

> returnfunction(mat)$value
Error in returnfunction(mat)$value : 
  $ operator is invalid for atomic vectors
> returnfunction(mat)$index
Error in returnfunction(mat)$index : 
  $ operator is invalid for atomic vectors


the "returnfunction(mat)$value" should be 4,5,10
the "returnfunction(mat)$index" should be 3,1,2

Thank you in advance
 

ZhaoXing
Department of Health Statistics
West China School of Public Health
Sichuan University
No.17 Section 3, South Renmin Road
Chengdu, Sichuan 610041
P.R.China

__________________________________________________
¸Ï¿ì×¢²áÑÅ»¢³¬´óÈÝÁ¿Ãâ·ÑÓÊÏä?


----------------------------------------------------------------------------
NOTICE: Please note that this eMail, and the contents thereof, 
is subject to the standard Sasol eMail legal notice which may be found at: 
http://www.sasol.com/legalnotices                                                                                                          

If you cannot access the legal notice through the URL attached and you wish 
to receive a copy thereof please send an eMail to 
legalnotice at sasol.com
----------------------------------------------------------------------------
-------------- next part --------------
______________________________________________
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.

----------------------------------------------------------------------------
NOTICE: Please note that this eMail, and the contents thereof, 
is subject to the standard Sasol eMail legal notice which may be found at: 
http://www.sasol.com/legalnotices                                                                                                          

If you cannot access the legal notice through the URL attached and you wish 
to receive a copy thereof please send an eMail to 
legalnotice at sasol.com
----------------------------------------------------------------------------


More information about the R-help mailing list