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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:26:33+00:00 2026-06-14T12:26:33+00:00

Just a quick question on how to handle outputs of different lengths using ldply

  • 0

Just a quick question on how to handle outputs of different lengths using ldply from the plyr package. Here is a simple version of the code I am using and the error I am getting:

# function to collect the coefficients from the regression models:
> SecreatWeapon <- dlply(merged1,~country.x, function(df) {
+     lm(log(child_mortality) ~ log(IHME_usd_gdppc)+ hiv_prev,data=df)
+ })
> 
# functions to extract the output of interest
> extract.coefs <- function(mod) c(extract.coefs = summary(mod)$coefficients[,1])
> extract.se.coefs <- function(mod) c(extract.se.coefs = summary(mod)$coefficients[,2])
> 
# function to combine the extracted output
> res <- ldply(SecreatWeapon, extract.coefs)
Error in list_to_dataframe(res, attr(.data, "split_labels")) : 
 Results do not have equal lengths

Here the error is due to the fact that some models will contain NA values so that:

> SecreatWeapon[[1]]

Call:
lm(formula = log(child_mortality) ~ log(IHME_usd_gdppc) + hiv_prev, 
    data = df)

Coefficients:
       (Intercept)  log(IHME_usd_gdppc)             hiv_prev  
           -4.6811               0.5195                   NA  

and therefore the following output won’t have the same length; for example:

> summary(SecreatWeapon[[1]])$coefficients
                  Estimate Std. Error   t value     Pr(>|t|)
(Intercept)         -4.6811000  0.6954918 -6.730633 6.494799e-08
log(IHME_usd_gdppc)  0.5194643  0.1224292  4.242977 1.417349e-04

but for the other one I get

> summary(SecreatWeapon[[10]])$coefficients
                   Estimate  Std. Error    t value     Pr(>|t|)
(Intercept)           18.612698   1.7505236  10.632646 1.176347e-12
log(IHME_usd_gdppc)   -2.256465   0.1773498 -12.723244 6.919009e-15
hiv_prev            -272.558951 160.3704493  -1.699558 9.784053e-02

Any easy fixes? Thank you very much,

Antonio Pedro.

  • 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-14T12:26:34+00:00Added an answer on June 14, 2026 at 12:26 pm

    The summary.lm( . ) function accessed with $coefficients gives different output than the coef would with an lm argument for any lm-object with an NA “coefficient”. Would you be satisfied with using something like this:

    coef.se <- function(mod) {
          extract.coefs <- function(mod) coef(mod) # lengths all the same
          extract.se.coefs <- function(mod) { summary(mod)$coefficients[,2]}
    return( merge( extract.coefs(mod), extract.se.coefs(mod), by='row.names', all=TRUE) ) 
                 }
    

    With Roland’s example it gives:

    > coef.se(fit)
        Row.names          x         y
    1 (Intercept) -0.3606557 0.1602034
    2          x1  2.2131148 0.1419714
    3          x2         NA        NA
    

    You could rename the x as coef and the y as se.coef

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

Sidebar

Related Questions

just a quick question, was trying different ways to accomplish this, but ended with
Just a quick question here. I have a program in which I need to
just a quick question! i am forcing my site to redirect from http to
Just a quick question, wondering if objects retrieved from the service container in Symfony2
Just a quick question... I've got a free and paid version of an app.
Just a (hopefully) quick question to clear something up with how Javascript handles objects.
Just a quick question, how do I get pypy to recognize third pary modules
Just a quick question.. Following opening a text file in a separate definition (no
Just a quick question about ReadWriteLocks in Java (specifically the ReentrantReadWriteLock implementation) as I
Just a quick question. Say a call a method like so mysql_pconnect(server,tator_w,password) or die(Unable

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.