user1912925
user1912925

Reputation: 781

Correct indexing of named vectors

This is a very quick and simple question that I am sure has been asked repreatedly on here. However after 45 minutes of searching I cannot answer the problem. Please link to whichever is the relevant question or delete it if needed.

I have the following:

>str(slope)
List of 55
$ : Named num [1:2] -0.00044 0.00311
..- attr(*, "names")= chr [1:2] "(Intercept)" "y"
$ : Named num [1:2] 1.374 -0.0276
..- attr(*, "names")= chr [1:2] "(Intercept)" "y"
$ : Named num [1:2] 3.704 -0.102
..- attr(*, "names")= chr [1:2] "(Intercept)" "y"
$ : Named num [1:2] 9.275 -0.294
..- attr(*, "names")= chr [1:2] "(Intercept)" "y"
$ : Named num [1:2] 15.76 -0.46
..- attr(*, "names")= chr [1:2] "(Intercept)" "y"
$ : Named num [1:2] 16.27 -0.443
..- attr(*, "names")= chr [1:2] "(Intercept)" "y"
$ : Named num [1:2] 25.973 -0.717

How do access all values of "y" e.g. if I want to plot them? I can access individual "y" values using:

> slope[[ c(1, 2) ]]
[1] 0.003111922

but not all of them at once.

Upvotes: 1

Views: 55

Answers (2)

Aaron - mostly inactive
Aaron - mostly inactive

Reputation: 37754

sapply(slope, `[`, 2)

Also try

foo <- do.call(rbind, slope)
foo[,2]

Upvotes: 1

Jilber Urbina
Jilber Urbina

Reputation: 61154

Try using sapply and [[:

sapply(slope, '[[', "y")

or maybe

sapply(slope, '[[', 2)

If it doesn't work, then provide a reproducible example and some data.

Upvotes: 1

Related Questions