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

Remove Rows With Same Value Across All Columns Using R

I have the following dataset, and I need to remove rows if they are all empty or have same value across all the columns:

df <- data.frame(players=c('', 'Uncredited', 'C', 'D', 'E'),
                 assists=c("", "Uncredited", 4, 4, 3),
                 ratings=c("", "Uncredited", 4, 7, ""))


df

players      assists      ratings
<chr>         <chr>        <chr>
        
Uncredited  Uncredited  Uncredited
  C             4            4
  D             4            7
  E             3                                                                    ​               ​        ​

In our example, the 1st row is all empty and the 2nd row has the same value of Uncredited. Hence, the 1st two rows would be removed.

Desired Output

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

players assists ratings
 <chr>  <dbl>   <chr>
  C       4       4
  D       4       7
  E       3 

Any suggestions would be appreciated. Thanks!

>Solution :

You can use apply to loop over all rows and filter for those that have more than a single distinct value. Note that if all value in a row are empty the row also has only one distinct value, so the first condition is part of the second condition.

df[apply(df,
         MARGIN = 1, # rowwise
         FUN = function(x) length(unique(x)) > 1), ]
#>   players assists ratings
#> 3       C       4       4
#> 4       D       4       7
#> 5       E       3
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