rpm
rpm

Reputation: 111

Extracting unnamed(?) elements from a list in R

Browse[1]> lmc
[[1]]

t test of coefficients:

                   Estimate Std. Error t value Pr(>|t|)
(Intercept)       0.0090841  0.0063588  1.4286 0.154840
m[, "FX_RET_28"]  0.1122490  0.1599463  0.7018 0.483705
m[, "FX_RET_42"]  0.1702606  0.1041854  1.6342 0.103944
m[, "FX_RET_51"] -0.4735956  0.2450406 -1.9327 0.054823 .
m[, "FX_RET_52"]  0.2475292  0.1458240  1.6975 0.091321 .
m[, "FX_RET_53"] -0.5569527  0.1945823 -2.8623 0.004699 **
m[, "FX_RET_60"] -0.3191905  0.2887157 -1.1056 0.270379
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Browse[1]> str(lmc)
List of 1
 $ : coeftest [1:7, 1:4] 0.00908 0.11225 0.17026 -0.4736 0.24753 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:7] "(Intercept)" "m[, \"FX_RET_28\"]" "m[, \"FX_RET_42\"]" "m[, \"FX_RET_51\"]" ...
  .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
  ..- attr(*, "method")= chr "t test of coefficients"

I want to pull out the Estimate column into a vector with (Intercept), m[, "FX_RE_28"], etc. as the names of the elements. I would appreciate any help.

Thanks

Upvotes: 1

Views: 2269

Answers (1)

IRTFM
IRTFM

Reputation: 263471

From the str output, one would predict that the values for the Estimate-column could be extracted with one of four incantations:

 lmc[[1]][ , 1]   # using just numerical indexing ... OR
 # Apparently not this: lmc[['coeftest']][ , "Estimate" ]  # Using character/name indexing

From the comments it appears that there was no name of the list element. Appears the "coeftest" is not a name but rather the class-type of the first (and only=) item in that list.

I thought (but was wrong) : The reason that lmc[[1]]$coeftest[,1] offered by RichAtMango fails is that the object is a list and lmc[[1]] delivers the first and only item of the list which is a matrix. This would have worked: lmc[1]$coeftest[,1] , because the [.]-function delivers a sublist (rather than the value itself) and it still would have had an element named 'coeftest'.

If you had wanted a one column matrix (which would display with the rownames on the side) then the call would have been:

lmc[['coeftest']][ , "Estimate" , drop=FALSE] # to avoid returning as a vector

You cannot "accept" comments as answers in SO. It's not clear why MichaelChirico didn't post an answer. He may have been too busy to post something he thought was sufficiently developed or maybe he wanted to to post dput(lmc) so he could offer a tested answer. I thought the downvote you got was unfair, since you did provide enough information to answer and the indexing difference between "[" and "[[" can be difficult to get for persons beginning with R. Your request needed understanding of both the indexing of lists and the indexing of R matrices.

Upvotes: 1

Related Questions