r - Create list with depth equal 2 -
i have list depth equal 2. subscript list described in following example:
my.list<-list( a=list( a1=1:10, a2=11:20), b=list( b1=21:30, b2=31:40), c=list( c1=41:50, c2=51:60) ) # choose specific item my.list[[2]][[2]][[3]] # choose first items a1,a2,b1,b2,c1,c2 test<-list() for(i in 1:3) (j in 1:2)test[[i]][[j]]<-my.list[[i]][[j]][[1]] test error in `*tmp*`[[i]] : subscript out of bounds
but won't work since list created has no lists within it. works this:
test<-list() for(i in 1:3) (j in 1:2)test[[i]]<-my.list[[i]][[j]][[1]]
and then
test<-list() for(i in 1:3) (j in 1:2)test[[i]][[j]]<-my.list[[i]][[j]][[1]] test
gives:
[[1]] [1] 1 11 [[2]] [1] 21 31 [[3]] [1] 41 51
...
does following code solve problem?
lapply(my.list, sapply, head, 1)
gives first element of each vector each entry in my.list
:
$a a1 a2 1 11 $b b1 b2 21 31 $c c1 c2 41 51
Comments
Post a Comment