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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:25:31+00:00 2026-05-15T04:25:31+00:00

Thanks to Hadley’s plyr package ddply function we can take a dataframe, break it

  • 0

Thanks to Hadley’s plyr package ddply function we can take a dataframe, break it down into subdataframes by factors, send each to a function, and then combine the function results for each subdataframe into a new dataframe.

But what if the function returns an object of a class like glm or in my case, a c(“glm”, “lm”). Then, these can’t be combined into a dataframe can they? I get this error instead

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class 'c("glm", "lm")' into a data.frame

Is there some more flexible data structure that will accommodate all the complex glm class results of my function calls, preserving the information regarding the dataframe subsets?

Or should this be done in an entirely different way?

  • 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-05-15T04:25:32+00:00Added an answer on May 15, 2026 at 4:25 am

    Just to expand my comment: plyr has set of functions to combine input and output type. So when you function returns something inconvertible to data.frame you should use list as output. So instead of using ddply use dlply.

    When you want to do something on each model and convert results to data.frame then ldply is the key.

    Lets create some models using dlply

    list_of_models <- dlply(warpbreaks, .(tension), function(X) lm(breaks~wool, data=X))
    str(list_of_models, 1)
    # List of 3
    #  $ L:List of 13
    #   ..- attr(*, "class")= chr "lm"
    #  $ M:List of 13
    #   ..- attr(*, "class")= chr "lm"
    #  $ H:List of 13
    #   ..- attr(*, "class")= chr "lm"
    #  - attr(*, "split_type")= chr "data.frame"
    #  - attr(*, "split_labels")='data.frame':        3 obs. of  1 variable:
    

    It gives list of three lm models.

    Using ldply you could create a data.frame, e.g.

    • with predictions of each model:

      ldply(list_of_models, function(model) {
          data.frame(fit=predict(model, warpbreaks))
      })
      #     tension     fit
      # 1         L 44.5556
      # 2         L 44.5556
      # 3         L 44.5556
      
    • with statistics to each model:

      ldply(list_of_models, function(model) {
        c(
          aic = extractAIC(model),
          deviance = deviance(model),
          logLik = logLik(model),
          confint = confint(model),
          coef = coef(model)
        )
      })
      # tension aic1    aic2 deviance   logLik confint1  confint2 confint3 confint4 coef.(Intercept) coef.woolB
      # 1       L    2 98.3291  3397.78 -72.7054  34.2580 -30.89623  54.8531 -1.77044          44.5556  -16.33333
      # 2       M    2 81.1948  1311.56 -64.1383  17.6022  -4.27003  30.3978 13.82559          24.0000    4.77778
      # 3       H    2 76.9457  1035.78 -62.0137  18.8701 -13.81829  30.2411  2.26273          24.5556   -5.77778
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks in advance to anyone who can think of a more efficient or a
Thanks in Advance.... Can any one knows, I have a requirement to perfrom OnclickListener
Thanks for previous replies, I am very new to Urban_Airship and integrating this into
Thanks in advance. Getting this warning when using below code: Warning: file_get_contents(test.php) [function.file-get-contents]: failed
Thanks in advance. Can you please let me know if there are any delegate
Thanks to a fantastic video on youtube which you can access here if you
Thanks in advance for the assistance. I recently ran into an issue with a
Thanks for the help on here with my query on here the other day
Thanks for stopping by. this static analyser warning is annoying me here is my
Thanks to the epic work of Dennis Williamson, I am now able to calculate

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.