[R] stop calculation in a function

Berend Hasselman bhh at xs4all.nl
Thu May 10 13:51:57 CEST 2012


On 10-05-2012, at 12:59, Sarah Goslee wrote:

> Well, if i goes from 2 to length(x) and you try to access x[i+1], of
> course odd things will happen. Why not construct the loop to
> (length(x)-1)  instead, so that x[i+1] is defined.
> 

The reference to x[i+1] in a commented line so I don't think this  is the cause.

The reason for the behaviour can be seen when indenting the code for the for loop part properly:

    for (i in 2:length(x)){
       if((!is.na(x[i]))){
               if((tps[i]-tps[ind_temp] < ecart_temps) & (abs(x[i]-temp) > seuil)){
               #&(abs(x[i+1]-x[i])<1))
               st1[i] <- NA
           }
           else {
               temp <- st1[i] <- x[i]
               ind_temp <- i
           }
       }
    }
    return(st1)

When is.na(x[i]) == TRUE then the st1 entry for that value of i is skipped.
So when x has trailing NA's st1 will be shorter.

It's better to declare st1 initially of the correct length

st1 <- numeric(length(x))

which could also speed things up when x is very long (because st1 has to be increased every time i increases).

Berend


> Sarah
> 
> 
> On Thu, May 10, 2012 at 5:14 AM, jeff6868
> <geoffrey_klein at etu.u-bourgogne.fr> wrote:
>> Hi dear R-users,
>> 
>> I have a question about a function I'm trying to improve.
>> How can I stop the function calculation at the last numeric value of my
>> data?
>> The problem is that the end of my data contains missing values (NAs). And
>> the aim of my function is to compare the first numeric value with the next
>> one (till the end). For the moment, It works well when my data doesn't
>> contains any NAs at the end of my file. I think that the problem is, as I
>> have NAs at the end of my data, R tries to compare my last numeric value
>> with the next numeric value wich doesn't exists, and so tries to modify the
>> length of my data (the error message is that the output has not the same
>> length as the input).
>> Could somebody tell me what I should modify or add in my function in order
>> to fix this problem?
>> Here's the function. Thanks for your advises!
>> 
>> out2NA <- function(x,seuil){
>>    st1 = NULL
>>    # Temporal variable memorysing the last "correct" numeric value#
>>    temp <- st1[1] <- x[1]
>>    ind_temp <- 1
>>    # Max time gap between two comparisons #
>>    ecart_temps <- 10
>>    tps <- time(x)
>> 
>>    for (i in 2:length(x)){
>>    if((!is.na(x[i]))){
>>    if((tps[i]-tps[ind_temp] < ecart_temps) & (abs(x[i]-temp) > seuil)){
>>    #&(abs(x[i+1]-x[i])<1)){
>>    st1[i] <- NA
>>    }
>>    else {
>>    temp <- st1[i] <- x[i]
>>    ind_temp <- i
>>    }
>>    }
>>    }
>>    return(st1)
>>    }
>> 
>>    dat1 <- myts[,2]
>>    myts[,2] <- apply(dat1,2,function(x) out2NA(x,2))
>> 
>> --
> -- 
> Sarah Goslee
> http://www.functionaldiversity.org
> 
> ______________________________________________
> 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