Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:26:50+00:00 2026-06-04T07:26:50+00:00

I’m working on a data set that includes community data, and many of the

  • 0

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 which() to remove rows by factors, e.g.,

September<-which(data$Time_point=="September")

data<-data[-September,] 

and the two ways I’ve tried removing columns is by using apply():

data<-data[,apply(data,2,function(x)sum(x<=15))]

and by using a messy for loop/if else combo:

for (i in 6:length(data)){
    if (sum(data[,i])<=15)
    data[,i]<-NULL
    else 
    data[,i]<-data[,i]
    }

Neither of these methods has been working. Surely there is an elegant way to get rid of columns based on logical criteria?

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:26:52+00:00Added an answer on June 4, 2026 at 7:26 am

    What about a simple subset? First, we create a simple data frameL

    R> dd = data.frame(x = runif(5), y = 20*runif(5), z=20*runif(5))
    

    Then select the columns where the sum is greater than 15

    R> dd1 = dd[,colSums(dd) > 15]
    R> ncol(dd1)
    [1] 2
    

    In your data set, you only want to subset columns 6 onwards, so something like:

     ##Drop the first five columns
     dd[,colSums(dd[,6:ncol(dd)]) > 15]
    

    or

     #Keep the first six columns
     cols_to_drop = c(rep(TRUE, 5), dd[,6:ncol(dd)]>15)
     dd[,cols_to_drop]
    

    should work.


    The key part to note is that in the square brackets, we want a vector of logicals, i.e. a vector of TRUE and FALSE. So if you wanted to subset using something a bit more complicated, then create a function that returns TRUE or FALSE and subset as usual.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.