# [R] Odp: I need help

Petr PIKAL petr.pikal at precheza.cz
Wed May 2 14:48:03 CEST 2007

```Hi
r-help-bounces at stat.math.ethz.ch napsal dne 02.05.2007 14:11:51:

> hello,
> I need help because I don't understand the syntaxe "else" how can I
write it
> for example I writed a script to cut missings values and I have errors
>
> > if(na==length(C)){
> + pos=match(0,match(donGeno[[na-1]],donGeno[[na]],nomatch=0))
> +               for(k in 1:(na-1)) {
> +                                              if(pos==1) {donGeno[[k]]
<-
> donGeno[[k]][2:C[k]]}
> +        if(pos==C[k]){donGeno[[k]] <- donGeno[[k]][1:(C[k]-1)]}
> +        else{donGeno[[k]] <-
c(donGeno[[k]][1:(pos-1)],donGeno[[k]][(pos+1):C(k)])}
> +             }
> +                }
> > if(na==1){
> + pos=match(0,match(donGeno[[na+1]],donGeno[[na]],nomatch=0))
> + for(k in 2:length(C)){
> + if(pos==1) {donGeno[[k]] <- donGeno[[k]][2:C[k]]}
> + if(pos==C[k]){donGeno[[k]] <- donGeno[[k]][1:(C[k]-1)]}
> + else{donGeno[[k]] <-
c(donGeno[[k]][1:(pos-1)],donGeno[[k]][(pos+1):C(k)])}
> +      }
> +       }
> > else{for(k in 1:(na-1)){
> Erreur : erreur de syntaxe dans "else"
> >   if(pos==1) {donGeno[[k]] <- donGeno[[k]][2:C[k]]}
> >   if(pos==C[k]){donGeno[[k]] <- donGeno[[k]][1:(C[k]-1)]}
> >   else{donGeno[[k]] <-
c(donGeno[[k]][1:(pos-1)],donGeno[[k]][(pos+1):C(k)])}
> Erreur : erreur de syntaxe dans "  else"
> >     }
> Erreur : erreur de syntaxe dans "    }"
> >           for(k in 1:(na-1)){
> +    if(pos==1) {donGeno[[k]] <- donGeno[[k]][2:C[k]]}
> +   if(pos==C[k]){donGeno[[k]] <- donGeno[[k]][1:(C[k]-1)]}
> +   else{donGeno[[k]] <-
c(donGeno[[k]][1:(pos-1)],donGeno[[k]][(pos+1):C(k)])}
> +      }
> Erreur dans C(k) : objet non interprétable comme un facteur
> >             }
> Erreur : erreur de syntaxe dans "            }"
>
>
> Have you got some ideas?
>

What about to try to provide some reproducible example as suggested in
posting guide. I believe your messy code can be evaluated in much more
neat and concise way without so many ifs and fors. Maybe you can uncover
some by yourself what trying to write a simple reproducible example. I am
reluctant to decipher what you want to achieve but maybe you want retain
only common values of several sets. So e.g. from match help page

## The intersection of two sets :
intersect <- function(x, y) y[match(x, y, nomatch = 0)]
> x<-sample(1:100, 50)
> y<-1:50
> x2<-sample(1:100,50)

> intersect(x,x2)
[1] 39 87 66  7 64 79 62 98  6 95 96 35 74 36  3 50 58 97 52 33 61 88 47
17 32 11 76 25
> intersect(y,intersect(x,x2))
[1]  3  6  7 11 17 25 32 33 35 36 39 47 50

Regarding the error message

> if (1==1) print(25) else print(30)
[1] 25
> if (1==2) print(25) else print(30)
[1] 30
> if (1==1) print(25)
[1] 25
> else print(30)
Error: syntax error, unexpected ELSE in "else"
>

>From help page
In particular, you ***should not have a newline between } and else to
avoid a syntax error*** in entering a if ... else construct at the
keyboard or via source. For that reason, one
(somewhat extreme) attitude of defensive programming is to always use
braces, e.g., for if clauses

Regards
Petr

>
>
>
___________________________________________________________________________
>
>
>
>
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help