> -----Original Message----- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Duncan Murdoch > Sent: Monday, April 05, 2010 8:24 AM > To: anna > Cc: r-help@r-project.org > Subject: Re: [R] Deleting many list elements in one time > > On 05/04/2010 11:17 AM, anna wrote: > > Hi guys, here is a simple thing I want to do but it doesn't work: > > I have a vector of the element indexes that I want to > delete called index > > so when I write myList[[index]] <- NULL to delete these > elements here is > > what I get: > > Error in myList[[index]] <- NULL : > > more elements supplied than there are to replace > > Isn't it possible to delete multiple elements? > > If your index is a list of numerical indices as opposed to > names, then > just do > > myList <- myList[-index] > > the same way you'd delete elements from any vector. > > If index is a vector of names, you need to convert it to > numbers, using > something like > > indexnums <- which( names(myList) %in% index ) > > and then use myList[-indexnums].
If none of names(myList) are in the index vector then indexnums will be integer(0) and myList[-integer(0)] will be the same as myList[integer(0)] which has length zero. Hence you should check for length(indexnums)>0 before doing the subscripting. Logical subscripting avoids that trap isNameInIndex <- names(myList) %in% index myList[!isNameInIndex] (Read the '[' as 'such that'). Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > > Duncan Murdoch > > ______________________________________________ > 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. > ______________________________________________ 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.