Skip to content

Logical subset of the columns in a dataframe

3 messages · Mark, Brian Ripley, David Winsemius

#
On Wed, 28 Jan 2009, Mark Na wrote:

            
Use something like

     DF[sapply(DF, function(x) mean(x) >= 0.01)]

Since logical values are converted to 0/1, mean() gives the frequency 
(and sum() the count).

  
    
#
One approach to such a problem would be to use a logical vector inside  
the function colSums.

?colSums

 > DF <- data.frame(XX= runif(20), YY=runif(20))

 > colSums(DF > 0.5)
XX YY
11  9

 > colSums(DF > -Inf)
XX YY
20 20
 >
 > colSums(DF> 0.5)/colSums(DF > -Inf) #could have used DF >= min(DF)  
in the denominator
   XX   YY
0.55 0.45