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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:14:52+00:00 2026-06-03T01:14:52+00:00

Apologies for such a rudimentary question–I must be missing something obvious. I want to

  • 0

Apologies for such a rudimentary question–I must be missing something obvious.

I want to build a list of lm objects, which I’m then going to use in an llply call to perform mediation analysis on this list. But this is immaterial–I just first want to make a list of length m (where m is the set of models) and each element within m will itself contain n lm objects.

So in this simple example

d1 <- data.frame(x1 = runif(100, 0, 1),
             x2 = runif(100, 0, 1),
             x3 = runif(100, 0, 1),
             y1 = runif(100, 0, 1),
             y2 = runif(100, 0, 1),
             y3 = runif(100, 0, 1))

m1 <- lm(y1 ~ x1 + x2 + x3, data = d1)
m2 <- lm(x1 ~ x2 + x3, data = d1)
m3 <- lm(y2 ~ x1 + x2 + x3, data = d1)
m4 <- lm(x2 ~ x1 + x3, data = d1)
m5 <- lm(y3 ~ y1 + y2 + x3, data = d1)
m6 <- lm(x3 ~ x1 + x2, data = d1)

I want a list containing 3 elements, and the first element will contain m1 and m2, the second will contain m3 and m4, etc. My initial attempt is sort of right, but the lmm objects don’t retain their class.

mlist <- list(c(m1,m2),
              c(m3,m4),
              c(m5,m6))

It has the right length (ie length(mlist) equals 3), but I thought I could access the lm object itself with

class(mlist[1][[1]])

but this element is apparently a list.

Am I screwing up how I build the list in the first step, or is this something more fundamental regarding lm objects?

  • 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-03T01:14:53+00:00Added an answer on June 3, 2026 at 1:14 am

    No, you’re just getting confused with c and list indexing. Try this:

    mlist <- list(list(m1,m2),
                  list(m3,m4),
                  list(m5,m6))
    > class(mlist[[1]][[1]])
    [1] "lm"
    

    So c will concatenate lists by flattening them. In the case of a lm object, that basically means it’s flattening each lm object in a list of each of the object components, and then concatenating all those lists together. c is more intuitively used on atomic vectors.

    The indexing of lists often trips people up. The thing to remember is that [ will always return a sub-list, while [[ selects an element.

    In my example above, this means that mlist[1] will return a list of length one. That first element is still a list. So you’d have to do something like mlist[1][[1]][[1]] to get all the way down to the lm object that way.

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

Sidebar

Related Questions

Apologies for asking such a primitive question. I wrote a function which took one
My apologies for asking such a novice question but, I need help building a
First I want to apologize for such a silly question but I'm quite new
Apologies for asking what's probably a very obvious question, I've been banging my head
Possible Duplicate: Python conditional assignment operator Apologies for such a simple question, but googling
I am having dumb monday so my apologies for posting such a newbie-like question.
My apologies in advance for posting such a lengthy question. Believe it or not,
Apologies to ask such a basic question but I really cant see why In3
Apologies for such a basic question but I can't figure it out. I know
Apologies if I'm missing the obvious... I am plotting a 3d surface with rgl.

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.