Home > other >  Extracting index value from a list in R?
Extracting index value from a list in R?

Time:05-07

I want to extract the random effects from my lmer model, including the person this random effect belongs to. My goal is to create a tibble that has one column for the person and another column for the random effect.

Using coef(modelA)$bib I am able to extract the random effect to a list. Here I also see which person the random effect belongs to.

> coef(modelA)$bib
    (Intercept)
31   0.37031060
32   0.49877575
33   0.50586345
34   0.52036187
35   0.49813250

However, adding this to a tibble, this information is lost.

> tibble(randEffectModA)
# A tibble: 65 x 1
   `(Intercept)`
           <dbl>
 1         0.370
 2         0.499
 3         0.506
 4         0.520
 5         0.498

Is there a simple way to solve this problem?

CodePudding user response:

Those are rownames and tibbles do not support rownames.

You have few options -

  1. Keep the information in a dataframe instead of tibble so the rownames are maintained.
result <- data.frame(coef(modelA)$bib)
  1. Create the rownames as separate column if you want to use tibbles.
randEffectModA <- data.frame(coef(modelA)$bib)

result <- tibble::tibble(person_no = rownames(randEffectModA), 
                         intercept = unlist(randEffectModA))
  • Related