On 11/09/2014, 6:04 AM, PO SU wrote: > > > Actually, i thought the way: > a<-1:3 > b<-NULL or 2 > a[-b] will not work if b is NULL > > A<-c(a,1)
If a is a long vector, that is a very expensive operation, at least as expensive as constructing a logical vector of the same length as a. > B<-c(b,length(A)) > A[-B] will get the same result as if b is NULL get a, if b is 2 get a[-2] > > I think it works well in considering memory use or efficiency or code tidy. Understanding R memory use is hard: be sure to profile it before you decide on the efficiency of one operation over another. Duncan Murdoch > > > > > -- > > PO SU > mail: [email protected] > Majored in Statistics from SJTU > > > > > At 2014-09-11 05:59:42, "PO SU" <[email protected]> wrote: >> >> Orignally i don't want to do the if ( length) check because i know that in a >> 10000 loops, after may be 10 or 20 or 100 loops , "i" will not be empty. >> so i mean , in the left loops, i would always check something not needed to >> check which i would not like to do. >> >> >> >> -- >> >> PO SU >> mail: [email protected] >> Majored in Statistics from SJTU >> >> >> >> >> At 2014-09-11 05:38:12, "Duncan Murdoch" <[email protected]> wrote: >>> On 10/09/2014, 9:53 PM, PO SU wrote: >>>> >>>> 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) >>> >>> If you know that you want to omit elements 1 and 3, then negative >>> integer indexing is safe. You'll never need to explicitly type out the >>> whole logical vector. >>> >>> The only problem with it is when you construct the indices by tests, and >>> sometimes nothing matches the tests, so you end up with an empty vector >>> or NULL. In that case you can just as easily construct the logical >>> vector. >>> >>> If your vectors are so long that you are worried about the cost of >>> constructing a logical vector that long, then it's probably worthwhile >>> checking the length of the integer index explicitly, i.e. instead of >>> >>> a <- a[ -i ] >>> >>> use >>> >>> if (any(i > 0)) a <- a[ -i ] >>> >>> Duncan Murdoch >>> >>> >>>> 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: [email protected] >>>> Majored in Statistics from SJTU >>>> >>>> >>>> >>>> >>>> At 2014-09-11 01:58:46, "Duncan Murdoch" <[email protected]> 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 <[email protected]> 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: [email protected] >>>>>>> Majored in Statistics from SJTU >>>>>>> >>>>>>> >>>>>>> >>>>>>> At 2014-09-10 06:45:59, "Duncan Murdoch" <[email protected]> >>>>>>> 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 >>>>>>>> >>>>>>> ______________________________________________ >>>>>>> [email protected] 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. >>>>> >>> ______________________________________________ [email protected] 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.

