Dear fellow R users, I am replacing elements of a list like so: pulse_subset[[1:20]]=unlist(pulse[i])[1:20] where pulse is a list of lists, and pulse [i] has >20 values. This gives the error "Recursive Indexing failed at level 2". But, interestingly this instruction is part of a loop which has gone through about 200,000 iterations before giving this error. Actual code: > pulse_subset[[1:(length(unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])]))]] <- unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])] Error in pulse_subset[[1:(length(unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])]))]] <- unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])] : recursive indexing failed at level 2 If anyone could shed some light I'd be rather grateful. Regards, Jim Hargreaves
On 26/06/2010 7:53 AM, Jim Hargreaves wrote:> Dear fellow R users, > > I am replacing elements of a list like so: > > pulse_subset[[1:20]]=unlist(pulse[i])[1:20] >If pulse is a list, then pulse[i] is also a list, with one element. I think you want pulse[[i]], which extracts element i. If pulse_subset is a list, then pulse_subset[[1:20]] is equivalent to pulse_subset[[1]][[2]][[3]][[4]] ... [[20]], i.e. the syntax implies that it is a list containing a list etc, nested 20 levels deep. The error message is telling you that it's not. I'm not sure what your intention is in this case. Duncan Murdoch> where pulse is a list of lists, and pulse [i] has >20 values. > > This gives the error "Recursive Indexing failed at level 2". But, > interestingly this instruction is part of a loop which has gone through > about 200,000 iterations before giving this error. > > Actual code: > > > pulse_subset[[1:(length(unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])]))]] > <- unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])] > > Error in > pulse_subset[[1:(length(unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])]))]] > <- unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])] : > recursive indexing failed at level 2 > > If anyone could shed some light I'd be rather grateful. > > Regards, > Jim Hargreaves > > ______________________________________________ > R-help at 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. >
Why do you use *double* square brackets on the left side of the replacement?>From the help info for "[[":"The most important distinction between [, [[ and $ is that the [ can select more than one element whereas the other two select a single element." You seem to be selecting 20 elements. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Hargreaves Sent: Saturday, 26 June 2010 9:54 PM To: r-help at r-project.org Subject: [R] Recursive indexing failed at level 2 Dear fellow R users, I am replacing elements of a list like so: pulse_subset[[1:20]]=unlist(pulse[i])[1:20] where pulse is a list of lists, and pulse [i] has >20 values. This gives the error "Recursive Indexing failed at level 2". But, interestingly this instruction is part of a loop which has gone through about 200,000 iterations before giving this error. Actual code: > pulse_subset[[1:(length(unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])]))]] <- unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])] Error in pulse_subset[[1:(length(unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])]))]] <- unlist(pulse[i])[as.numeric(peak_start[i]):as.numeric(peak_end[i])] : recursive indexing failed at level 2 If anyone could shed some light I'd be rather grateful. Regards, Jim Hargreaves ______________________________________________ R-help at 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.