mardi 7 novembre 2017

if function as sequence in loop function

I want to use an if-function in the "sequence part" of a loop function. Let's imagine the data frame looks something like this:

aa<-c(1,2,3,3,4,4,4,5,6) bb<-c(1,0,1,0,1,1,0,0,0) cc<-c(1,0,1,0,2,3,1,0,0) df = data.frame(aa,bb,cc)

Put in words, I would like the loop to be executed and the result eventually stored incc every time that aa takes the values 2:5

What I unsuccessfully came up looks like this:

x<-(2:5)

for(i in df$aa(x)){ if(df$bb %in%1 & df$cc%in%1) { result<-cc} }

Any leads appreciated!

Aucun commentaire:

Enregistrer un commentaire