Create a table from the last lines of the list of xts objects

I have a list of xts objects. I would like to get data.frame with the last lines of each level of the list.

My list looks like this:

a <- xts(matrix(1:4,2),as.Date("2012-01-01")+0:1)
b <-list(a,a*5)
> b
[[1]]
           [,1] [,2]
2012-01-01    1    3
2012-01-02    2    4

[[2]]
           [,1] [,2]
2012-01-01    5   15
2012-01-02   10   20

I would like to get:

[,1] [,2]
2    4
10   20

Column names are the same for each level of the list.

+3
source share
2 answers

In addition to @Henrik's answer, you can also use a combination of lapply, do.calland last:

do.call(rbind,lapply(b,last))
+2
source

Since bthis is a list, you can use any member of the family *apply, for example:

vapply(b, "[", i = 2, rep(0,2))

If the objects in bhave different numbers of rows, then the following should work:

vapply(b, function(x) x[nrow(x),], rep(0,ncol(b[[1]])))

, 2 rep(0,2), , .

, t , :

t(vapply(b, "[", i = 2, rep(0,2)))
+1

All Articles