:) Well, yes, but what do you do with a named vector if you want to 
remove an element by name?

It is not general: you cannot do that on vectors, matrices, arrays and 
all inherited objects anyway. Using a negative index is a standard and 
throughout practice of deleting elements in R. Surely one can have 
exceptions or extended behaviour for different classes, like list and 
data.frame here, but I cannot say it is really necessary to have it in 
order to produce a clean, easily readable and reliable code. Yes, I know 
now, this NULL assignment has existed there for long time and I am not 
about to propose its removal, but I really do not see a good reason for 
having it either. I would never use it in my code either. After all you 
do not assign NULLs to elements of a non-list vector or matrix or array.

Best,
Oleg

Gabor Grothendieck wrote:
> But what about by name?
> 
> a <- list(a = 1, b = 2, c = 3)
> 
> a$b <- NULL
> 
> 
> On Feb 13, 2008 9:39 AM, Oleg Sklyar <[EMAIL PROTECTED]> wrote:
>> Hmm, I think the pretty traditional R style does the job...
>>
>> a = list(1,2,3)
>> a[-2]
>>
>> So I really do not see a good reason for doing a[2] = NULL instead of a
>> = a[-2]
>>
>>
>> Jeffrey J. Hallman wrote:
>>> >From your tone, I gather you don't much like this behavior, and I can see 
>>> >your
>>> point, as it not very intuitive that setting a list element to NULL deletes
>>> any existing element at that index.  But is there a better way to delete an
>>> element from a list?  Maybe there should be.
>>>
>>> Jeff
>>>
>>> Prof Brian Ripley <[EMAIL PROTECTED]> writes:
>>>>> I have just came across an (unexpected to me) behaviour of lists when
>>>>> assigning NULLs to list elements. I understand that a NULL is a valid R
>>>>> object, thus assigning a NULL to a list element should yield exactly the
>>>>> same result as assigning any other object. So I was surprised when
>>>>> assigning a NULL in fact removed the element from the list. Is this an
>>>>> intended behaviour? If so, does anybody know where is it documented and
>>>>> what is a good way around?
>>>> Yes, it was apparently intended: R has long done this.
>>>>
>>>> x <- list(a=c(1L,2L), b=matrix(runif(4),2,2), c=LETTERS[1:3])
>>>> x[2] <- list(NULL)
>>>>
>>>> is what I think you are intending.
>>>>
>>>> See e.g. the comment in subassign.c
>>>>
>>>>          /* If "val" is NULL, this is an element deletion */
>>>>          /* if there is a match to "nlist" otherwise "x" */
>>>>          /* is unchanged.  The attributes need adjustment. */
>> --
>> Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466
>>
>> ______________________________________________
>>
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>

-- 
Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to