marlene marchena
2011-Sep-16 15:52 UTC
[R] problems to report indexes when I have two min value
Hi, I need to repor the index of a min value of each row in a matrix, but I don't know how to do that when I have more than one min value. Here is my example> dat <-matrix(c(5.4,4.8,5.6,4.8,NA,4.4,4.6,3.4,NA,NA,4,2.4,NA,NA,NA,2),byrow=TRUE,ncol=4)> dat[,1] [,2] [,3] [,4] [1,] 5.4 4.8 5.6 4.8 [2,] NA 4.4 4.6 3.4 [3,] NA NA 4.0 2.4 [4,] NA NA NA 2.0> k <- apply(dat, 1, function(x) which(x == min(x, na.rm = TRUE))) > k[[1]] [1] 2 4 [[2]] [1] 4 [[3]] [1] 4 [[4]] [1] 4 But I need an output like this k<- 2 or 4, 4, 4, 4 Someone could help me with this issue. Thanks in advance, Marlene. [[alternative HTML version deleted]]
Jeff Newmiller
2011-Sep-16 16:05 UTC
[R] problems to report indexes when I have two min value
Your coded answer looks right, and is represented in a syntactically correct manner. Your "desired" answer looks ambiguous, and is certainly not syntactically correct R. If you want ambiguous and vague answers, perhaps you need to keep a human in the loop. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. marlene marchena <marchenamarlene@gmail.com> wrote: Hi, I need to repor the index of a min value of each row in a matrix, but I don't know how to do that when I have more than one min value. Here is my example> dat <-matrix(c(5.4,4.8,5.6,4.8,NA,4.4,4.6,3.4,NA,NA,4,2.4,NA,NA,NA,2),byrow=TRUE,ncol=4)> dat[,1] [,2] [,3] [,4] [1,] 5.4 4.8 5.6 4.8 [2,] NA 4.4 4.6 3.4 [3,] NA NA 4.0 2.4 [4,] NA NA NA 2.0> k <- apply(dat, 1, function(x) which(x == min(x, na.rm = TRUE))) > k[[1]] [1] 2 4 [[2]] [1] 4 [[3]] [1] 4 [[4]] [1] 4 But I need an output like this k<- 2 or 4, 4, 4, 4 Someone could help me with this issue. Thanks in advance, Marlene. [[alternative HTML version deleted]] _____________________________________________ 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]]
Jean-Christophe BOUËTTÉ
2011-Sep-16 16:13 UTC
[R] problems to report indexes when I have two min value
Hi there, Assuming you would prefer to have only one min per row: apply(dat,1,which.min) there is a link to this function in the help page for ?min. JC 2011/9/16 marlene marchena <marchenamarlene at gmail.com>:> Hi, > > I need to repor the index of a min value of each row in a matrix, but I > don't know how to do that when I have more than one min value. > > Here is my example > >> dat <- > matrix(c(5.4,4.8,5.6,4.8,NA,4.4,4.6,3.4,NA,NA,4,2.4,NA,NA,NA,2),byrow=TRUE,ncol=4) > >> dat > ? ? [,1] [,2] [,3] [,4] > [1,] ?5.4 ?4.8 ?5.6 ?4.8 > [2,] ? NA ?4.4 ?4.6 ?3.4 > [3,] ? NA ? NA ?4.0 ?2.4 > [4,] ? NA ? NA ? NA ?2.0 >> k <- apply(dat, 1, function(x) which(x == min(x, na.rm = TRUE))) >> ? ?k > [[1]] > [1] 2 4 > > [[2]] > [1] 4 > > [[3]] > [1] 4 > > [[4]] > [1] 4 > > But I need an output like this > > k<- 2 or 4, 4, 4, 4 > > Someone could help me with this issue. > > Thanks in advance, > > Marlene. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >