Search code examples
rsubsetmultiple-columnszero

R subset columns that meets a condition of zero ratio


I have a dataset named df,

Here I want to get the columns (make a new dataset with them), that have a zero ratio of > %50

df_new <- get columns where zero_ratio> %50

could you support?

Thank You


Solution

  • Try with colMeans :

    df_new <- df[, colMeans(df == 0, na.rm = TRUE) > 0.5]
    

    With a reproducible example :

    df <- data.frame(a = c(1, 2, 0, 1, 3), b = c(0, 0, 1, 0, 1), c = 0)
    df_new <- df[, colMeans(df == 0, na.rm = TRUE) > 0.5]
    df_new
    
    #  b c
    #1 0 0
    #2 0 0
    #3 1 0
    #4 0 0
    #5 1 0