Re: [Rd] assigning NULLs to elements of a list

2008-02-13 Thread Oleg Sklyar
:) 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


Re: [Rd] assigning NULLs to elements of a list

2008-02-13 Thread Gabor Grothendieck
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


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


Re: [Rd] assigning NULLs to elements of a list

2008-02-13 Thread Oleg Sklyar
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


Re: [Rd] assigning NULLs to elements of a list

2008-02-12 Thread Thomas Lumley
On Tue, 12 Feb 2008, Oleg Sklyar wrote:

 Dear developers:

 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?

Yes.

  If so, does anybody know where is it documented and
 what is a good way around?


One place is FAQ 7.1

   -thomas


Thomas Lumley   Assoc. Professor, Biostatistics
[EMAIL PROTECTED]   University of Washington, Seattle

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


Re: [Rd] assigning NULLs to elements of a list

2008-02-12 Thread Jeffrey J. Hallman
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. */

-- 
Jeff

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


Re: [Rd] assigning NULLs to elements of a list

2008-02-12 Thread Stephen B. Weston
Oleg Sklyar wrote:
 Dear developers:
 
 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?

This topic is discussed in the R FAQ, in section 7.1 How can I set 
components of a list to NULL?  It also shows up in section 3.3.3,
in the context of incompatibilities with S.

- Steve

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


Re: [Rd] assigning NULLs to elements of a list

2008-02-12 Thread Prof Brian Ripley
On Tue, 12 Feb 2008, Oleg Sklyar wrote:

 Dear developers:

 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. */



 Thanks for help,
 Oleg Sklyar

 Here goes an example, the inline C-code does exactly what I would prefer
 R were doing, but both R examples do remove the element 'b':

 x = list(a=c(1L,2L), b=matrix(runif(4),2,2), c=LETTERS[1:3])

 library(inline)
 code=
   SEXP res;
   PROTECT(res = Rf_duplicate(x));
   SET_VECTOR_ELT(res,1,R_NilValue);
   UNPROTECT(1);
   return res;
 
 setnull = cfunction(signature(x=list),code)
 setnull(x)

 # $a
 # [1] 1 2
 # $b
 # NULL
 # $c
 # [1] A B C

 y = x
 x[[2]] = NULL
 x
 # $a
 # [1] 1 2
 # $c
 # [1] A B C

 x = y
 x$b = NULL
 x
 # $a
 # [1] 1 2
 # $c
 # [1] A B C

  sessionInfo()
 R version 2.6.1 (2007-11-26)
 i686-pc-linux-gnu

 locale:
 LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=en_GB.UTF-8;LC_MONETARY=en_GB.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATION=C

 attached base packages:
 [1] stats graphics  grDevices utils datasets  methods   base

 other attached packages:
 [1] inline_0.3.3

 loaded via a namespace (and not attached):
 [1] rcompgen_0.1-17


 --
 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


-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

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


Re: [Rd] assigning NULLs to elements of a list

2008-02-12 Thread Dan Davison
On Tue, Feb 12, 2008 at 11:06:59AM +, Oleg Sklyar wrote:
 Dear developers:
 
 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?
 
 Thanks for help,
 Oleg Sklyar
 
 Here goes an example, the inline C-code does exactly what I would prefer 
 R were doing, but both R examples do remove the element 'b':
 
 x = list(a=c(1L,2L), b=matrix(runif(4),2,2), c=LETTERS[1:3])
 
 library(inline)
 code=
SEXP res;
PROTECT(res = Rf_duplicate(x));
SET_VECTOR_ELT(res,1,R_NilValue);
UNPROTECT(1);
return res;
 
 setnull = cfunction(signature(x=list),code)
 setnull(x)
 
 # $a
 # [1] 1 2
 # $b
 # NULL
 # $c
 # [1] A B C
 
 y = x
 x[[2]] = NULL

Hi Oleg,

To do this I use

x[2] - list(NULL)

In particular I do this when constructing lists to act as the 'what' argument 
for scan(), where
a NULL element means that the column should be skipped, whereas obviously a 
non-existent element
won't serve that purpose. I would be interested to hear whether this is the 
'proper' way to construct
such lists.

Dan



 x
 # $a
 # [1] 1 2
 # $c
 # [1] A B C
 
 x = y
 x$b = NULL
 x
 # $a
 # [1] 1 2
 # $c
 # [1] A B C
 
   sessionInfo()
 R version 2.6.1 (2007-11-26)
 i686-pc-linux-gnu
 
 locale:
 LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=en_GB.UTF-8;LC_MONETARY=en_GB.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATION=C
 
 attached base packages:
 [1] stats graphics  grDevices utils datasets  methods   base
 
 other attached packages:
 [1] inline_0.3.3
 
 loaded via a namespace (and not attached):
 [1] rcompgen_0.1-17
 
 
 -- 
 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

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


[Rd] assigning NULLs to elements of a list

2008-02-12 Thread Oleg Sklyar
Dear developers:

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?

Thanks for help,
Oleg Sklyar

Here goes an example, the inline C-code does exactly what I would prefer 
R were doing, but both R examples do remove the element 'b':

x = list(a=c(1L,2L), b=matrix(runif(4),2,2), c=LETTERS[1:3])

library(inline)
code=
   SEXP res;
   PROTECT(res = Rf_duplicate(x));
   SET_VECTOR_ELT(res,1,R_NilValue);
   UNPROTECT(1);
   return res;

setnull = cfunction(signature(x=list),code)
setnull(x)

# $a
# [1] 1 2
# $b
# NULL
# $c
# [1] A B C

y = x
x[[2]] = NULL
x
# $a
# [1] 1 2
# $c
# [1] A B C

x = y
x$b = NULL
x
# $a
# [1] 1 2
# $c
# [1] A B C

  sessionInfo()
R version 2.6.1 (2007-11-26)
i686-pc-linux-gnu

locale:
LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=en_GB.UTF-8;LC_MONETARY=en_GB.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

other attached packages:
[1] inline_0.3.3

loaded via a namespace (and not attached):
[1] rcompgen_0.1-17


-- 
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