title: Excluding columns from a dataframe based on column sums
body: <p>I'm working on a data set that includes community data, and many of the columns (species) have a lot of zeroes. I would like to be able to drop these columns for some of the analyses I'm doing, based on the sum of the whole column.
  I'm tempted to do this with a for loop, but I hear that the apply and by functions are better when you're using R.
  My goal is to remove all columns with a sum of less than 15.
  I have used <code>which()</code> to remove rows by factors, e.g.,</p>
  
  <pre><code>September&lt;-which(data$Time_point=="September")
  
  data&lt;-data[-September,] 
  </code></pre>
  
  <p>and the two ways I've tried removing columns is by using <code>apply()</code>:</p>
  
  <pre><code>data&lt;-data[,apply(data,2,function(x)sum(x&lt;=15))]
  </code></pre>
  
  <p>and by using a messy for loop/if else combo:</p>
  
  <pre><code>for (i in 6:length(data)){
      if (sum(data[,i])&lt;=15)
      data[,i]&lt;-NULL
      else 
      data[,i]&lt;-data[,i]
      }
  </code></pre>
  
  <p>Neither of these methods has been working. Surely there is an elegant way to get rid of columns based on logical criteria?</p>
  
  <pre><code>str(head(data,10))
  'data.frame':   10 obs. of  23 variables:
   $ Core_num    : Factor w/ 159 levels "152","153","154",..: 133 72 70 75 89 85 86 90 95 99
   $ Cage_num    : num  0 1 2 3 4 5 6 7 8 9
   $ Treatment   : Factor w/ 4 levels "","C","CC","NC": 1 2 2 2 2 2 2 2 2 2
   $ Site        : Factor w/ 10 levels "","B","B07","B08",..: 1 8 8 8 7 7 7 7 9 9
   $ Time_point  : Factor w/ 3 levels "","May","September": 1 2 2 2 2 2 2 2 2 2
   $ Spionidae   : num  108 0 0 0 0 0 0 0 0 0
   $ Syllidae    : num  185 0 0 0 3 8 0 1 4 1
   $ Opheliidae  : num  424 0 1 0 0 0 1 1 0 0
   $ Cossuridae  : num  164 0 7 3 0 0 0 0 0 0
   $ Sternaspidae: num  214 0 0 6 1 0 11 9 0 0
   $ Sabellidae  : num  1154 0 2 2 0 ...
   $ Capitellidae: num  256 1 10 17 0 3 0 0 0 0
   $ Dorvillidae : num  21 1 0 0 0 0 0 0 0 0
   $ Cirratulidae: num  17 0 0 0 0 0 0 0 0 0
   $ Oligochaeta : num  3747 12 41 27 32 ...
   $ Nematoda    : num  410 5 4 13 0 0 0 2 2 0
   $ Sipuncula   : num  33 0 0 0 0 0 0 0 0 0
   $ Ostracoda   : num  335 0 1 0 0 0 0 0 0 0
   $ Decapoda    : num  62 0 4 0 1 0 0 0 0 0
   $ Amphipoda   : num  2789 75 17 34 89 ...
   $ Copepoda    : num  75 0 0 0 0 0 0 0 0 0
   $ Tanaidacea  : num  84 0 0 0 1 0 0 0 0 0
   $ Mollusca    : int  55 0 4 0 0 0 0 0 0 0
  </code></pre>