# How to assign a value with highest matches as TRUE?

Hi

I want to write a code that compares column "Currentid" with "Matchid" to see if there's a perfect match (i.e., the "Matchp' is equal to 100.0), and if that is not the case, I still want the code to say that it is a perfect match (i.e., TRUE) if the number of correct matches are larger than the number of incorrect matches.

As you can see in my data below, there are perfect matches for all rows except 9. So the code should report a "TRUE" value . But if there were equal amounts of correct and incorrect matches, it should report false.

data.frame(
stringsAsFactors = FALSE,
Sampleid = c("AVM_360","AVM_360","AVM_360",
"AVM_360","AVM_360","AVM_360","AVM_360","AVM_360",
"AVM_360"),
Currentid = c("Bibasis vasutana",
"Bibasis vasutana","Bibasis vasutana","Bibasis vasutana",
"Bibasis vasutana","Bibasis vasutana","Bibasis vasutana",
"Bibasis vasutana","Bibasis vasutana"),
Matchp = c("100.0","100.0","100.0",
"100.0","100.0","100.0","100.0","100.0","100.0"),
Matchid = c("Bibasis vasutana",
"Bibasis vasutana","Bibasis vasutana","Bibasis vasutana",
"Bibasis vasutana","Bibasis vasutana","Bibasis vasutana",
"Bibasis vasutana","Bibasis nikos")
)

thank you!!!

Hi there,

Can you make the conditions slightly more clear for us? I tried to put together an example here of how you can create a new column based on 2 conditions. Testing first if `matchp = 100.0` and then secondly if the `Currentid == Matchid`. Your exact conditions could be made to fit this schema. Let me know if this works or helps.

``````library(tidyverse)

df <- data.frame(
stringsAsFactors = FALSE,
Sampleid = c("AVM_360","AVM_360","AVM_360",
"AVM_360","AVM_360","AVM_360","AVM_360","AVM_360",
"AVM_360"),
Currentid = c("Bibasis vasutana",
"Bibasis vasutana","Bibasis vasutana","Bibasis vasutana",
"Bibasis vasutana","Bibasis vasutana","Bibasis vasutana",
"Bibasis vasutana","Bibasis vasutana"),
Matchp = c("100.0","100.0","100.0",
"100.0","100.0","100.0","100.0","100.0","100.0"),
Matchid = c("Bibasis vasutana",
"Bibasis vasutana","Bibasis vasutana","Bibasis vasutana",
"Bibasis vasutana","Bibasis vasutana","Bibasis vasutana",
"Bibasis vasutana","Bibasis nikos")
)

df2 <- df %>%
dplyr::mutate(perfect_match = ifelse(Matchp == "100.0", TRUE,
ifelse(Currentid == Matchid, TRUE, FALSE)
)
)

df2
#>   Sampleid        Currentid Matchp          Matchid perfect_match
#> 1  AVM_360 Bibasis vasutana  100.0 Bibasis vasutana          TRUE
#> 2  AVM_360 Bibasis vasutana  100.0 Bibasis vasutana          TRUE
#> 3  AVM_360 Bibasis vasutana  100.0 Bibasis vasutana          TRUE
#> 4  AVM_360 Bibasis vasutana  100.0 Bibasis vasutana          TRUE
#> 5  AVM_360 Bibasis vasutana  100.0 Bibasis vasutana          TRUE
#> 6  AVM_360 Bibasis vasutana  100.0 Bibasis vasutana          TRUE
#> 7  AVM_360 Bibasis vasutana  100.0 Bibasis vasutana          TRUE
#> 8  AVM_360 Bibasis vasutana  100.0 Bibasis vasutana          TRUE
#> 9  AVM_360 Bibasis vasutana  100.0    Bibasis nikos          TRUE
``````

Created on 2022-03-03 by the reprex package (v2.0.1)

Hi!

Your solution didn't quite work (I also realize my question was a bit unclear), however, I managed to figure it out based on the ifelse function you mentioned, so thank you for that!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.