Subset and lagging list data structure R
Posted
by
user1234440
on Stack Overflow
See other posts from Stack Overflow
or by user1234440
Published on 2012-12-03T06:35:36Z
Indexed on
2012/12/03
17:04 UTC
Read the original article
Hit count: 242
I have a list that is indexed like the following:
>list.stuff
[[1]]
[[1]]$vector
...
[[1]]$matrix
....
[[1]]$vector
[[2]]
null
[[3]]
[[3]]$vector
...
[[3]]$matrix
....
[[3]]$vector
.
.
.
Each segment in the list is indexed according to another vector of indexes:
>index.list
1, 3, 5, 10, 15
In list.stuff
, only at each of the indexes 1,3,5,10,15 will there be 2 vectors and one matrix; everything else will be null like [[2]]
. What I want to do is to lag like the lag.xts
function so that whatever is stored in [[1]]
will be pushed to [[3]]
and the last one drops off. This also requires subsetting the list, if its possible. I was wondering if there exists some functions that handle list manipulation.
My thinking is that for xts
, a time series can be extracted based on an index you supply:
xts.object[index,] #returns the rows 1,3,5,10,15
From here I can lag it with:
lag.xts(xts.object[index,])
Any help would be appreciated thanks:
EDIT: Here is a reproducible example:
list.stuff<-list()
vec<-c(1,2,3,4,5,6,7,8,9)
vec2<-c(1,2,3,4,5,6,7,8,9)
mat<-matrix(c(1,2,3,4,5,6,7,8),4,2)
list.vec.mat<-list(vec=vec,mat=mat,vec2=vec2)
ind<-c(2,4,6,8,10)
for(i in ind){
list.stuff[[i]]<-list.vec.mat
}
© Stack Overflow or respective owner