How to remove rows where all columns are zero using dplyr pipe

前端 未结 5 703
醉梦人生
醉梦人生 2021-01-18 07:41

I have the following data frame:

dat <- structure(list(`A-XXX` = c(1.51653275922944, 0.077037240321129, 
0), `fBM-         


        
5条回答
  •  轮回少年
    2021-01-18 08:32

    Here's another option using the row-wise operations of dplyr (with col1,col2,col3 defining three exemplary columns for which the rowwise sum is calculated):

    library(tidyverse)
    
    df <- df %>% 
        rowwise() %>% 
        filter(sum(c(col1,col2,col3)) != 0)
    

    Alternatively, if you have tons of variables (columns) to select you can also use the tidyverse selection syntax via:

    df <- df %>% 
        rowwise() %>% 
        filter(sum(c_across(col1:col3)) != 0)
    

    For details see: https://dplyr.tidyverse.org/articles/rowwise.html

提交回复
热议问题