RE: [R] subscripts in lists

2003-08-14 Thread Liaw, Andy
sapply(lis, function(x) x[which(x == next) + 1]) [1] want1 want2 HTH, Andy From: Chris Knight I am tying myself in knots over subscripts when applied to lists I have a list along the lines of: lis-list(c(a,b,next,want1,c),c(d, next, want2, a)) From which I want to extract the

Re: [R] subscripts in lists

2003-08-14 Thread Richard A. O'Keefe
I suggested sapply(1:length(lis), function (i) {v - lis[[i]]; v[which(v==next)+1]}) Of course that was really dumb. It can be simplified, because the index i is only used to select a list element, which sapply() wants to do for me anyway. It should be sapply(lis,

Re: [R] subscripts in lists

2003-08-14 Thread M.Kondrin
Chris Knight wrote: I am tying myself in knots over subscripts when applied to lists I have a list along the lines of: lis-list(c(a,b,next,want1,c),c(d, next, want2, a)) From which I want to extract the values following next in each member of the list, i.e. something along the lines of

Re: [R] subscripts in lists

2003-08-14 Thread Richard A. O'Keefe
Chris Knight [EMAIL PROTECTED] has lis-list(c(a,b,next,want1,c),c(d, next, want2, a)) and wants c(want1,want2) Step 1: inx - sapply(lis, function(x) which(x == next)) + 1 == 4 3 Step 2: sapply(1:length(lis), function(i) lis[[i]][inx[i]]) == want1 want2 Think

Re: [R] subscripts in lists

2003-08-14 Thread Peter Wolf
What about ... unlist(lis)[which(unlist(lis)==next)+1] [1] want1 want2 ... to avoid the loop in sapply? Richard A. O'Keefe wrote: Chris Knight [EMAIL PROTECTED] has lis-list(c(a,b,next,want1,c),c(d, next, want2, a)) and wants c(want1,want2) Step 1: inx -

Re: [R] subscripts in lists

2003-08-12 Thread Prof Brian Ripley
On Tue, 12 Aug 2003, Peter Wolf wrote: What about ... unlist(lis)[which(unlist(lis)==next)+1] [1] want1 want2 ... to avoid the loop in sapply? That has a loop in each unlist, of course. (A slicker version of that solution was posted earlier by Richard.) Note that you have to loop