jeudi 27 avril 2017

Conditioning in R

I would like to implement a different counters based on different values of a certain variable:

Let's assume, I have a variable:

Age 

Now, I want to update different counters based on the value of 'Age'.

So, I can use the following code:

    if ( age<15) {
         count1<-count1+1
    } else if ( age<30) {
         count2<-count2+1
    } else if ( age<45) {
         count3<-count3 +1
    } else
         count4+1

Is there a nicer way to do it in R?

Aucun commentaire:

Enregistrer un commentaire