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 7847233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:49:51+00:00 2026-06-02T17:49:51+00:00

I am trying to reshape/ reduce my data. So far, I employ a for

  • 0

I am trying to reshape/ reduce my data. So far, I employ a for loop (very slow) but from what I perceive, this should be quite fast with Plyr.

I have many groups (firms, as a factor in the dataset) and I want to drop entirely every firm which shows a 0 entry for value in any of that firm’s cells. I thus create a new data.frame but leave out all groups showing 0 for value at some point.

The forloop:

Data Creation:

set.seed(1) 
mydf <- data.frame(firmname = sample(LETTERS[1:5], 40, replace = TRUE), 
        value = rpois(40, 2))

-----------------------------
splitby = mydf$firmname


new.data <- data.frame()

for (i in 1:(length(unique(splitby)))) {
temp <- subset(mydf, splitby == as.character(paste(unique(splitby)[i]))) 
    if (all(temp$value > 0) == "TRUE") {     
    new.data <- rbind(new.data, temp) 
} 
} 

Delete all empty firm factors 
new.data$splitby <- factor(new.data$splitby)

Is there a way to achieve that with the plyr package? Can the subset function be used in that context?

EDIT: For the purpose of the reproduction of the problem, data creation, as suggested by BenBarnes, is added. Ben, thanks a lot for that. Furthermore, my code is altered so as to comply with the answers provided below.

  • 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-02T17:49:53+00:00Added an answer on June 2, 2026 at 5:49 pm

    You could supply an anonymous function to the .fun argument in ddply():

    set.seed(1)
    
    mydf <- data.frame(firmname = sample(LETTERS[1:5], 40, replace = TRUE),
      value = rpois(40, 2))
    
    library(plyr)
    
    ddply(mydf,.(firmname), function(x) if(any(x$value==0)) NULL else x )
    

    Or using [, as suggested by Andrie:

    firms0 <- unique(mydf$firmname[which(mydf$value == 0)])
    
    mydf[-which(mydf$firmname %in% firms0), ]
    

    Note that the results of ddply are sorted according to firmname

    EDIT

    For the example in your comments, this approach is again faster than using ddply() to subset, selecting only firms with more than three entries:

    firmTable <- table(mydf$firmname)
    
    firmsGT3 <- names(firmTable)[firmTable > 3]
    
    mydf[mydf$firmname %in% firmsGT3, ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find some information on this but am unable to get any results
Trying to launch and pass tel. no. to skype by this code from my
Trying to match this data, that is contained in one string: vsan 1 interfaces:
I'm trying to convert some data from long to wide using the reshape2 package,
Trying to use this method (gist of which is use self.method_name in the FunnyHelper
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
I am trying to use the functionality of the reshape function available in MATLAB
Guys, I'm trying to finish up my homework but I'm having some problems here
I've been trying to import shape xml to customized View. like this, <shape xmlns:android=http://schemas.android.com/apk/res/android
Trying to pull a value from the sunrise element from [yweather:astronomy] in the Yahoo

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.