[R] some question about vector[-NULL]
William Dunlap
wdunlap at tibco.com
Wed Sep 10 18:20:24 CEST 2014
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).
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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list