Hi, I'm a new user. I've been reading through the manual and looking at various examples but am still trying to make sense of the most efficient ways to handle matrices of data. If I have a 2D matrix of data, how do I get the mean, min, max value of each row? I see the "mean" function on a matrix will give me averages by row, but min and max give me the value for the entire matrix. I want the min (for example) of each row. pmin looks useful, but I can't seem to get the syntax right to apply to each column. Right now I am doing this. Is there a one-liner that would work instead? minResult <- vector (mode="list", length=rowCount) for (row in 1:rowCount) { minResult[[row]] <- min(corResult[row], na.rm = TRUE) } Thanks in advance. WILL
Hello Will, z=matrix(c(1,2,3,4,5,6), 2,byrow=T) min=apply(z,1,min) what do you need isapply ?apply and its family are very helpful. apply( matrix, columns, function) apply( matrix, rows, function) Check ?apply Cheers Anna Anna Freni Sterrantino Ph.D Student Department of Statistics University of Bologna, Italy via Belle Arti 41, 40124 BO. ________________________________ Da: Will Glass-Husain <wglasshusain@gmail.com> A: r-help@stat.math.ethz.ch Inviato: Giovedì 29 gennaio 2009, 17:58:50 Oggetto: [R] Taking the min of each row in a matrix Hi, I'm a new user. I've been reading through the manual and looking at various examples but am still trying to make sense of the most efficient ways to handle matrices of data. If I have a 2D matrix of data, how do I get the mean, min, max value of each row? I see the "mean" function on a matrix will give me averages by row, but min and max give me the value for the entire matrix. I want the min (for example) of each row. pmin looks useful, but I can't seem to get the syntax right to apply to each column. Right now I am doing this. Is there a one-liner that would work instead? minResult <- vector (mode="list", length=rowCount) for (row in 1:rowCount) { minResult[[row]] <- min(corResult[row], na.rm = TRUE) } Thanks in advance. WILL ______________________________________________ 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. [[alternative HTML version deleted]]
?apply e.g. for the 'min' apply(yourMatrix, 1, min, na.rm=TRUE) On Thu, Jan 29, 2009 at 11:58 AM, Will Glass-Husain <wglasshusain at gmail.com> wrote:> Hi, > > I'm a new user. I've been reading through the manual and looking at > various examples but am still trying to make sense of the most > efficient ways to handle matrices of data. > > If I have a 2D matrix of data, how do I get the mean, min, max value > of each row? I see the "mean" function on a matrix will give me > averages by row, but min and max give me the value for the entire > matrix. I want the min (for example) of each row. pmin looks useful, > but I can't seem to get the syntax right to apply to each column. > > Right now I am doing this. Is there a one-liner that would work instead? > > minResult <- vector (mode="list", length=rowCount) > for (row in 1:rowCount) > { > minResult[[row]] <- min(corResult[row], na.rm = TRUE) > } > > Thanks in advance. > > WILL > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?