Hello, I have a question about list indexing. Lets say we have a list of 3 lists, each containing 3 different type elements:> a=replicate(3, list(list(c(1,1,1), diag(3), c(2,2,2)))) > a[[1]] [[1]][[1]] [1] 1 1 1 [[1]][[2]] [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 [[1]][[3]] [1] 2 2 2 [[2]] [[2]][[1]] [1] 1 1 1 [[2]][[2]] [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 [[2]][[3]] [1] 2 2 2 [[3]] [[3]][[1]] [1] 1 1 1 [[3]][[2]] [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 [[3]][[3]] [1] 2 2 2 If anyone can point a direction as to how to obtain (subset) the following list from list a:> b[[1]] [[1]][[1]] [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 [[1]][[2]] [1] 2 2 2 [[2]] [[2]][[1]] [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 [[2]][[2]] [1] 2 2 2 Also, suppose that one wishes to assign the list "b" to the corresponding subset of list "a". Is there a way of doing so? Thanks for the help. Ivo
Ivo Shterev wrote:> > > I have a question about list indexing. Lets say we have a list of 3 lists, > each containing 3 different type elements: > >(Details of your nice example code removed) a=replicate(3, list(list(c(1,1,1), diag(3), c(2,2,2)))) str(a) # I prefer this to print(a) because it shows the structure better # in steps b = lapply(a,function(x){ # print(str(a)) # put this here to know what you are doing x[2:3] } ) #... mmm, not yet what you wanted. Another try b[1:2] # I leave it as a easy exercise to jam this into a one-liner --- Dieter -- View this message in context: http://www.nabble.com/List-subsetting-tp24194764p24198222.html Sent from the R help mailing list archive at Nabble.com.
Another options is: head(lapply(a, tail, 2), 2) On Wed, Jun 24, 2009 at 8:42 PM, Ivo Shterev <idc318@yahoo.com> wrote:> > Hello, > > I have a question about list indexing. Lets say we have a list of 3 lists, > each containing 3 different type elements: > > > a=replicate(3, list(list(c(1,1,1), diag(3), c(2,2,2)))) > > a > [[1]] > [[1]][[1]] > [1] 1 1 1 > > [[1]][[2]] > [,1] [,2] [,3] > [1,] 1 0 0 > [2,] 0 1 0 > [3,] 0 0 1 > > [[1]][[3]] > [1] 2 2 2 > > > [[2]] > [[2]][[1]] > [1] 1 1 1 > > [[2]][[2]] > [,1] [,2] [,3] > [1,] 1 0 0 > [2,] 0 1 0 > [3,] 0 0 1 > > [[2]][[3]] > [1] 2 2 2 > > > [[3]] > [[3]][[1]] > [1] 1 1 1 > > [[3]][[2]] > [,1] [,2] [,3] > [1,] 1 0 0 > [2,] 0 1 0 > [3,] 0 0 1 > > [[3]][[3]] > [1] 2 2 2 > > > If anyone can point a direction as to how to obtain (subset) the following > list from list a: > > b > [[1]] > [[1]][[1]] > [,1] [,2] [,3] > [1,] 1 0 0 > [2,] 0 1 0 > [3,] 0 0 1 > > [[1]][[2]] > [1] 2 2 2 > > > [[2]] > [[2]][[1]] > [,1] [,2] [,3] > [1,] 1 0 0 > [2,] 0 1 0 > [3,] 0 0 1 > > [[2]][[2]] > [1] 2 2 2 > > Also, suppose that one wishes to assign the list "b" to the corresponding > subset of list "a". Is there a way of doing so? > > Thanks for the help. > Ivo > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Dear All, I am interested to know how assign a list to a list of lists. For example how to assign the list a = list(3,3,3) to the list b:> b[[1]] [[1]][[1]] [1] 0 [[1]][[2]] [1] 0 [[1]][[3]] [1] 0 [[2]] [[2]][[1]] [1] 0 [[2]][[2]] [1] 0 [[2]][[3]] [1] 0 [[3]] [[3]][[1]] [1] 0 [[3]][[2]] [1] 0 [[3]][[3]] [1] 0 so that after the assignment> b[[1]] [[1]][[1]] [1] 0 [[1]][[2]] [1] 0 [[1]][[3]] [1] 3 [[2]] [[2]][[1]] [1] 0 [[2]][[2]] [1] 0 [[2]][[3]] [1] 3 [[3]] [[3]][[1]] [1] 0 [[3]][[2]] [1] 0 [[3]][[3]] [1] 3 Thanks a lot! Ivo