I have a huge list, below is a sample of trboot6
$ 1 : num [1:39] -1 1 -1 -1 -1 -1 -1 -1 -1 1 ...
$ 2 : num [1:46] -1 -1 -1 1 1 1 -1 -1 -1 -1 ...
$ 3 : num [1:48] 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
$ 4 : num [1:43] -1 -1 1 -1 -1 -1 -1 -1 -1 -1 ...
What I want to do is check if in each list every pair has 1 and -1. Pairs are represented in brackets in the following:
$ 1 : num [1:39] (-1 1) (-1 -1) (-1 -1) (-1 -1) (-1 1) ...
$ 2 : num [1:46] (-1 -1) (-1 1) (1 1) (-1 -1) (-1 -1) ...
$ 3 : num [1:48] (1 -1) (-1 -1) (-1 -1) (-1 -1) (-1 -1) ...
$ 4 : num [1:43] (-1 -1) (1 -1) (-1 -1) (-1 -1) (-1 -1) ...
If the pair does not have 1 and -1 then, I want to delete the second same number, that is if the pair is (1 1), I delete the second 1. If there is 1 again, I delete this 1 too. Then if there is a -1, it will pair with the first 1.
To better code, I used the logic that the sum should always remain between -2 and 2 for the pairs to exist. Pair cant be (1,-1) (-1,1) or (1,-1) (1,-1). So if the balance goes <-2 or >2, the latest number has to be deleted.
Here is my code for the above logic:
balboot<-0
fboot<- function(x) {
if(x==-1){balboot<-balboot-1}
if(x==1){balboot<-+1}
if(balboot==-2){x<-0
balboot=-1}
if(balboot==2){x<-0
balboot=1}
return(fboot)
}
rdtp<-lapply(trboot6, FUN=fboot)
After running this, I get the warning:
In if (x == 1) { ... :
the condition has length > 1 and only the first element will be used
thank you for your help in advance.
Aucun commentaire:
Enregistrer un commentaire