I am trying to create a simple function to group the data I have based on a single variable in the data frame, called Depths.
I created the following function, but I'm still having issues.
vecki <- as.array(rep(0,50))
group_AtoD <- function(x){
i<- 1
for(i in 1:55){
if(x$Depths..m.[i] >= 0 & x$Depths..m. <= 1237.5){
vecki[i] <- "D"
i<- i+1
}else if(x$Depths..m.[i] > 1237.5 & x$Depths..m.[i] <= 2475){
vecki[i] <- "C"
i<- i+1
}else if(x$Depths..m.[i] > 2475 & x$Depths..m.[i] <= 3712.5){
vecki[i] <- "B"
i<- i+1
} else if(x$Depths..m.[i] > 3712.5 & x$Depths..m.[i] <= 4950) {
vecki[i] <- "A"
i<- i+1
}
}
return(cbind(x,vecki))
}
I get the following warning: "the condition has length > 1 and only the first element will be used"
I have seen the error appearing on other threads, but none of the advice or answers helped me in any way.
It ends up running, but the vecki vector I am creating and cbinding to my original data frame - in order to group - has only the value D. I ran i in the command line and it is 56 - so it is iterating fine.
Any help would be much appreciated.
Thanks!
EDIT:: I have solved the problem using a similar function to the one below and utilising which(). I just want to know, for educational purposes why the following function isnt working properly. Thanks again.