Hello! I'm working with a matrix called 'subER'. This matrix has 150(=h) rows and 15 columns. What I would like to do is have a command that will identify the minimum 4 values in each row and return the column numbers where these values were found. My hope is to store this information in a new 150 by 4 matrix called P. So far I've only been able to get R to identify one minimum value and return its corresponding column location: P=matrix(nrow=h, ncol=1) for(i in 1:h){ P[i,]=which.min(subER[i,]) } Any help would be greatly appreciated!! Thank you in advance, Jess -- View this message in context: http://r.789695.n4.nabble.com/Return-the-matrix-location-of-multiple-entries-tp4321412p4321412.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2012-Jan-23 18:08 UTC
[R] Return the matrix location of multiple entries
I'd do something like apply(subER, 1, function(x) which(x %in% sort(x)[1:4])) E.g. subER <- matrix(sample(100), 10) But I'll admit that seems clunkier than it should be. On Mon, Jan 23, 2012 at 12:41 PM, connollj <connollj at uoguelph.ca> wrote:> Hello! > > I'm working with a matrix called 'subER'. This matrix has 150(=h) rows and > 15 columns. What I would like to do is have a command that will identify the > minimum 4 values in each row and return the column numbers where these > values were found. My hope is to store this information in a new 150 by 4 > matrix called P. > > So far I've only been able to get R to identify one minimum value and return > its corresponding column location: > > P=matrix(nrow=h, ncol=1) > for(i in 1:h){ > P[i,]=which.min(subER[i,]) > } > > Any help would be greatly appreciated!! > > Thank you in advance, > > Jess > > > -- > View this message in context: http://r.789695.n4.nabble.com/Return-the-matrix-location-of-multiple-entries-tp4321412p4321412.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.