Update data frame using tidy functions

Viewed 33

Here is example data frame:

qseqid  sseqid  pident
NP_040593.1 J02459.1    100
NP_040593.1 JQ086376.1  100
NP_040593.1 JQ182735.1  100
NP_040594.1 NC_049953.1 99.306
NP_040594.1 OM418625.1  99.306
NP_040594.1 NC_049954.1 98.611
NP_040594.1 CP025712.1  98.611
NP_040594.1 OL657226.1  98.611

How to update dataframe to have distinct (using dplyr::distinct probably) pident==100, but keep all other pident values (including duplicates) the same?

So, to something like that:

qseqid  sseqid  pident
NP_040593.1 J02459.1    100
NP_040594.1 NC_049953.1 99.306
NP_040594.1 OM418625.1  99.306
NP_040594.1 NC_049954.1 98.611
NP_040594.1 CP025712.1  98.611
NP_040594.1 OL657226.1  98.611
1 Answers

We may use filter - create a logical condition with == on 100 value and change the TRUE to FALSE where that are duplicated or (|) to select all other other values (!=)

library(dplyr)
df1 %>% 
  filter(pident == 100 & !duplicated(pident)| pident != 100)

-output

      qseqid      sseqid  pident
1 NP_040593.1    J02459.1 100.000
2 NP_040594.1 NC_049953.1  99.306
3 NP_040594.1  OM418625.1  99.306
4 NP_040594.1 NC_049954.1  98.611
5 NP_040594.1  CP025712.1  98.611
6 NP_040594.1  OL657226.1  98.611

This may also be done with base R

subset(df1, pident == 100 & !duplicated(pident) | pident != 100)

data

df1 <- structure(list(qseqid = c("NP_040593.1", "NP_040593.1", "NP_040593.1", 
"NP_040594.1", "NP_040594.1", "NP_040594.1", "NP_040594.1", "NP_040594.1"
), sseqid = c("J02459.1", "JQ086376.1", "JQ182735.1", "NC_049953.1", 
"OM418625.1", "NC_049954.1", "CP025712.1", "OL657226.1"), pident = c(100, 
100, 100, 99.306, 99.306, 98.611, 98.611, 98.611)), 
class = "data.frame", row.names = c(NA, 
-8L))
Related