0

I have matrix containing two columns and many rows. The first column name is idCombinaison and the second column name is accuarcy. The accuarcy has a float values.

Now I want to get all rows which the value of accuarcy == max value. In some cases (like depicted in the picture), I can have many rows which the value of accuarcy equals to max, so I want to get all these rows!

I tried this:

maxAccuracy <- subset(accuarcyMatrix, accuarcyMatrix['accuarcy'] == max(accuarcyMatrix['accuarcy']))

But this return an empty vector. Any ideas please? enter image description here

nick
  • 1,090
  • 1
  • 11
  • 24
Mouaici_Med
  • 390
  • 2
  • 19
  • 1
    Please add the data using `dput()` instead of using a picture. – tmfmnk Jan 15 '19 at 21:10
  • 1
    If your data is a matrix, change `accuarcyMatrix['accuarcy']` to `accuarcyMatrix[, 'accuarcy']` in your code. – Darren Tsai Jan 15 '19 at 21:21
  • @DarrenTsai, Thank you very much, it is exactly the solution to my problem. if you want post your response in answers section, I will accept it. Thank you !! – Mouaici_Med Jan 15 '19 at 21:26

3 Answers3

1

A reproducible data simulating your matrix:

set.seed(123)
x <- matrix(sample(1:9, 30, T), 10, 3)
row.names(x) <- 1:10
colnames(x) <- LETTERS[1:3]

#    A B C
# 1  3 9 9
# 2  8 5 7
# 3  4 7 6
# ...

In matrix objects, you need to use a binary way to extract element such as data[a, b]. Take the above data for example, x["C"] will return NA and x[, "C"] will return all elements in column C. Therefore, the following two codes are going to generate different outputs.

subset(x, x["C"] == max(x["C"]))
#   A B C (Empty)

subset(x, x[, "C"] == max(x[, "C"]))
#   A B C
# 1 3 9 9
# 4 8 6 9
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
0

Maybe something like this?

library(dplyr)
accuarcyMatrix %>%
filter_at(vars(accuarcy),
          any_vars(.==max(.))
)
Baraliuh
  • 593
  • 3
  • 12
0

Base R solution (although this is very likely a duplicate):

accuarcyMatrix[ which(accuarcyMatrix$accuarcy == max(accuarcyMatrix$accuarcy) , ]

I'm guessing you will want to change "accuarcy" to "accuracy"

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Hey sir, I think your solution is not applicable to a matrix. I guess the OP's data is a matrix. – Darren Tsai Jan 15 '19 at 21:47
  • 1
    I'm not sure where you get that idea. He used `accuarcyMatrix['accuarcy']` which would probably only succeed for a dataframe and certainly would NOT succeed for a matrix whose rows were named as illustrated. – IRTFM Jan 15 '19 at 21:50
  • As shown in my answer, `matrix["name"]` will succeed but return an `NA`. And the OP said his code returns an empty vector, which matches what I reason that the data is a matrix. – Darren Tsai Jan 15 '19 at 22:03
  • In contrast, if the OP's data is a data frame, his code will work and return what he wants, not an empty vector. – Darren Tsai Jan 15 '19 at 22:07
  • Ah, the perils of attempting answers when there's no reproducible example. Yet another reason to leave the question closed. – IRTFM Jan 15 '19 at 22:09