# [R] Find solution for an error in the condition of if

Duncan Murdoch murdoch.duncan at gmail.com
Mon May 3 21:14:58 CEST 2010

```On 03/05/2010 2:43 PM, anderson nuel wrote:
> I  will sur that  problem  is coming from the first part of the test.
>
> data[pa,k] is a vector because pa is a vector.
>
> Coud you help me to solve this error.
>

If you want to test that all elements match, you should use

all(data[pa, k] == df[, j]) && data[ch, k] == i

The && says not to bother with the second test if the first one fails.

Duncan Murdoch

> Best Regards
>
>
>
>
> 2010/4/30 Duncan Murdoch <murdoch.duncan at gmail.com>
>
> > On 30/04/2010 4:19 AM, anderson nuel wrote:
> >
> >> Dear r-help,
> >>
> >> Could you help me to find a solution for this error:
> >>
> >> Il y a eu 50 avis ou plus (utilisez warnings() pour voir les 50 premiers)
> >>
> >>
> >>
> >>> warnings()
> >>>
> >>>
> >> Messages d'avis :
> >> 1: In if ((data[pa, k] == df[, j]) & (data[ch, k] == i)) { ... :
> >>  la condition a une longueur > 1 et seul le premier élément est utilisé
> >>
> >>
> >
> > It's saying that the condition has length > 1 and only the first element is
> > used.  This is probably coming from the first part of the test:  data[pa,k]
> > will be scalar, but df[,j] looks like a vector of every element in column j.
> >  The result is a vector with as many entries as you have rows in df.  Only
> > the first of them will be compared to data[pa, k].
> >
> > Duncan Murdoch
> >
> >> 2: In if ((data[pa, k] == df[, j]) & (data[ch, k] == i)) { ... :
> >>  la condition a une longueur > 1 et seul le premier élément est utilisé
> >>
> >>
> >> ch=3
> >> pa=c(1,2)
> >> r=2
> >> t=4
> >> nb=15
> >> ni=array(0,c( r,t))
> >>
> >> for ( i  in 1:r){
> >>
> >> for (j in 1:t ){
> >>
> >>    for (k in 1:nb){
> >>
> >>        if ( (data[pa,k]== df[,j]) & (data[ch,k]==i)){
> >>
> >>            ni[i,j]=ni[i,j]+1
> >>        }
> >>    }
> >> }
> >> }
> >>
> >> Best  Regards
> >>
> >>        [[alternative HTML version deleted]]
> >>
> >>  ------------------------------------------------------------------------
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help