# [R] some question about vector[-NULL]

Duncan Murdoch murdoch.duncan at gmail.com
Thu Sep 11 17:18:19 CEST 2014

```On 11/09/2014 3:29 AM, Philippe GROSJEAN wrote:
> On 11 Sep 2014, at 08:24, PIKAL Petr <petr.pikal at precheza.cz> wrote:
>
> > Hi
> >
> > You still do not disclose important info about details of your functions. However, when you want to perform indexing like you show, you maybe can get rid of NULL and use zero instead.
> >
> >> a<-1:5
> >> a[-c(1,3)]
> > [1] 2 4 5
> >> a[-c(0,1,3)]
> > [1] 2 4 5
> >> a[-c(1,0,3)]
> > [1] 2 4 5
> >> a[-c(0,1,0,3,0)]
> > [1] 2 4 5
> >
> > However I am almost sure that you are fishing in murky waters and what you do by cycle and fiddling with NULL elements can be achieved by more efficiently.
> >
> … and also look at this weird case:
>
> > a <- 1:5
> > a[-0]
> integer(0)
> > a[-c(0, 0)]
> integer(0)

What do you find to be weird about this?  As far as I can see it is
acting exactly as the documentation suggests it should:  -0 is treated
the same as 0.  Zero as an index selects nothing.  Two zeroes also
select nothing.  What's the surprise?

Duncan Murdoch

>
> Best,
>
> Philippe
>
>
> > Regards
> > Petr
> >
> >
> >> -----Original Message-----
> >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> >> project.org] On Behalf Of PO SU
> >> Sent: Thursday, September 11, 2014 3:54 AM
> >> To: Duncan Murdoch
> >> Cc: R. Help
> >> Subject: Re: [R] some question about vector[-NULL]
> >>
> >>
> >> Tks, i think using logical index is a way, but to do that, i have to
> >> keep a vector as long as the original vector. that's, to exclude
> >> position 1 and 3 from
> >> a<-1:5
> >> I have to let b<-c(F,T,F,T,T) and exec a[b], not a[-c(1,3)]. which
> >> c(1,3) is much shorter than b if a is a long vector. that's, b would
> >> be c(F,T,F,T,T,T,T,......,T) I thought a way ,
> >> let d<-c(a,1)
> >> that d<-c(1,2,3,4,5,1)
> >> and initialize the index vector   iv to length(d). that is iv<-6.
> >> then, d[-iv] is always equal  a[- i ] ,  whether i is NULL or not.
> >> Because if i is NULL ,then iv is 6, if i is 2.then iv is c(2,6) and so
> >> on.......
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >> --
> >>
> >> PO SU
> >> mail: desolator88 at 163.com
> >> Majored in Statistics from SJTU
> >>
> >>
> >>
> >>
> >> At 2014-09-11 01:58:46, "Duncan Murdoch" <murdoch.duncan at gmail.com>
> >> wrote:
> >>> On 10/09/2014 12:20 PM, William Dunlap wrote:
> >>>> Can you make your example a bit more concrete?  E.g., is your 'index
> >>>> vector' A an integer vector?  If so, integer(0), an integer vector
> >>>> with no elements, would be a more reasonable return value than NULL,
> >>>> an object of class NULL with length 0, for the 'not found' case and
> >>>> you could check for that case by asking if length(A)==0.
> >>>>
> >>>> Show us typical inputs and expected outputs for your function (i.e.,
> >>>> the problem you want to solve).
> >>>
> >>> I think the problem with integer(0) and NULL is the same:  a[-i]
> >> doesn't
> >>> act as expected (leaving out all the elements of i, i.e. nothing) if i
> >>> is either of those.  The solution is to use logical indexing, not
> >>> negative numerical indexing.
> >>>
> >>> Duncan Murdoch
> >>>>
> >>>> Bill Dunlap
> >>>> TIBCO Software
> >>>> wdunlap tibco.com
> >>>>
> >>>>
> >>>> On Wed, Sep 10, 2014 at 8:53 AM, PO SU <rhelpmaillist at 163.com>
> >> wrote:
> >>>>>
> >>>>> Tks for your
> >>>>>
> >>>>> a <- list(ress = 1, res = NULL)
> >>>>> And in my second question, let me explain it :
> >>>>> Actually i have two vectors in global enviroment, called A and B
> >> .A is initialized to NULL which used to record some index in B.
> >>>>> Then i would run a function F,  and each time, i would get a index
> >> value or NULL. that's,  D<-F(B). D would be NULL or  some index
> >> position in B.
> >>>>> But in the function F, though input is B,  i would exclude the
> >> index value from  B recorded in A. That's :
> >>>>> F<-function( B ) {
> >>>>> B<-B[-A]
> >>>>> some processing...
> >>>>> res<-NULL or some new index not included in A
> >>>>> return(res)
> >>>>> }
> >>>>> so in a loop,
> >>>>> A<-NULL
> >>>>> for( i in 1:100000) {
> >>>>> D<-F(B)
> >>>>> A<-c(A,D)
> >>>>> }
> >>>>> I never know whether D is a NULL or a different index  compared
> >> with indexes already recorded in A.
> >>>>> Actually, A<-c(A,D) work well, i never worry about whether D is
> >> NULL or a real index, but in the function F,  B<-B[-A] won't work.
> >>>>> so i hope that, e.g.
> >>>>> a<-1:3
> >>>>> a[-NULL] wouldn't trigger an error but return a.
> >>>>> Because, if i wrote function like the following:
> >>>>>
> >>>>> F<-function( B ) {
> >>>>> if( is.null(A))
> >>>>> B<-B
> >>>>> else
> >>>>> B<-B[-A]
> >>>>> some processing...
> >>>>> res<-NULL or some new index not included in A
> >>>>> return(res)
> >>>>> }
> >>>>> May be after 5 or 10 loops, A would already not NULL, so the added
> >> if ..else statement would be repeated in left  9999 loops which i would
> >> not like to see.
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>> --
> >>>>>
> >>>>> PO SU
> >>>>> mail: desolator88 at 163.com
> >>>>> Majored in Statistics from SJTU
> >>>>>
> >>>>>
> >>>>>
> >>>>> At 2014-09-10 06:45:59, "Duncan Murdoch"
> >> <murdoch.duncan at gmail.com> wrote:
> >>>>>> On 10/09/2014, 3:21 AM, PO SU wrote:
> >>>>>>>
> >>>>>>> Dear expeRts,
> >>>>>>>      I have some programming questions about NULL in R.There
> >> are listed as follows:
> >>>>>>> 1. I find i can't let a list have a element NULL:
> >>>>>>> a<-list()
> >>>>>>> a\$ress<-1
> >>>>>>> a\$res<-NULL
> >>>>>>> a
> >>>>>>> str(a)
> >>>>>>
> >>>>>> You can do it using
> >>>>>>
> >>>>>> a <- list(ress = 1, res = NULL)
> >>>>>>
> >>>>>>> How can i know i have a named element but it is NULL, not just
> >> get a\$xxxx,a\$iiii,a\$oooo there all get NULL
> >>>>>>
> >>>>>> That's a little harder.  There are a few ways:
> >>>>>>
> >>>>>> "res" %in% names(a) & is.null(a[["res"]])
> >>>>>>
> >>>>>> or
> >>>>>>
> >>>>>> identical(a["res"], list(res = NULL))
> >>>>>>
> >>>>>> or
> >>>>>>
> >>>>>> is.null(a[[2]])
> >>>>>>
> >>>>>> should all work.
> >>>>>>
> >>>>>> Generally because of the special handling needed, it's a bad idea
> >> to try
> >>>>>> to store NULL in a list.
> >>>>>>
> >>>>>>> 2.The most important thing:
> >>>>>>> a<-1:10
> >>>>>>> b<-NULL or 1
> >>>>>>> a<-c(a,b) will work so i don't need to know whether b is null or
> >> not,but:
> >>>>>>> a[-NULL] can't work!!  i just need a[-NULL]==a , how can i reach
> >> this purpose?
> >>>>>>
> >>>>>> Using !, and a logical test, e.g.
> >>>>>>
> >>>>>> a[!nullentry(a)]
> >>>>>>
> >>>>>> where nullentry() is a function based on one of the tests above,
> >> but
> >>>>>> applied to all entries.
> >>>>>>
> >>>>>> Duncan Murdoch
> >>>>>>
> >>>>> ______________________________________________
> >>>>> R-help at r-project.org mailing list
> >>>>> https://stat.ethz.ch/mailman/listinfo/r-help
> >> guide.html
> >>>>> and provide commented, minimal, self-contained, reproducible code.
> >>>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >
> > ________________________________
> > Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
> > Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
> > Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
> > Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.
> >
> > V případě, že je tento e-mail součástí obchodního jednání:
> > - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
> > - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
> > - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
> > - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.
> >
> > This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
> > If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
> > If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
> > The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.
> >
> > In case that this e-mail forms part of business dealings:
> > - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
> > - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
> > - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
> > - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help