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 __________________________________________________ ¸Ï¿ì×¢²áÑÅ»¢³¬´óÈÝÁ¿Ãâ·ÑÓÊÏä?
______________________________________________ R-help@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.