Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete rows with duplicate values and include NAs as individual values

I have a df like this:

testdf <- structure(list(POS = c(37, 44, 50, 83), Col1 = c("A", "C", NA, 
"G"), Col2 = c("A", NA, "T", "C")), class = "data.frame", row.names = c(NA, 
-4L))

which looks like that:

     POS  Col1 Col2
[1,] "37" "A"  "A" 
[2,] "44" "C"  NA  
[3,] "50" NA   "T" 
[4,] "83" "G"  "C" 

And i would like to exclude all rows which are the same between Col1 and Col2 (that means only row 1). Unfortunately i do not know how to deal with the NAs. When i try

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

testdf[testdf$Col1 != testdf$Col2,]

it does not consider NAs as an own entry?

The expected output should be:

     POS  Col1 Col2
[1,] "44" "C"  NA  
[2,] "50" NA   "T" 
[3,] "83" "G"  "C" 

I would rather not transform NAs into something else.

testdf %>%
  rowwise %>%
  filter(Col1 != Col2)

Is also not working correctly.

>Solution :

You can add is.na() to your filter condition.

You should also handle the case where both columns are NA; I added a row like this to your example data. If you want to keep these rows, then:

library(dplyr)

testdf %>%
  filter(is.na(Col1) | is.na(Col2) | Col1 != Col2)
  POS Col1 Col2
1  44    C <NA>
2  50 <NA>    T
3  83    G    C
4  99 <NA> <NA>

If you want to remove them, use xor() instead of |:

testdf %>%
  filter(xor(is.na(Col1), is.na(Col2)) |Col1 != Col2)
  POS Col1 Col2
1  44    C <NA>
2  50 <NA>    T
3  83    G    C
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading