Home > Net >  Select columns in dataset where at least 90 percent of the values are bigger than zero
Select columns in dataset where at least 90 percent of the values are bigger than zero

Time:11-02

I want to select the columns in a dataset where at least 90 percent of the values are bigger than zero. Would appreciate it if someone could show me a code that does this.

CodePudding user response:

A possible solution with R base:

Reproducible data example:

set.seed(1)
df <- as.data.frame(matrix(rnorm(100, 1.5), 10))

Solution:

df[colMeans(df > 0) > 0.9]

CodePudding user response:

We can use select(where).

library(dplyr)

df %>% select(where(~mean(.x>0)>0.9))
  • Related