Hi, The conditions are not very clear. For example, it is not mentioned whether vectors are of same length or not. Assuming the former case: fun1 <- function(x,y){ if(length(x)==length(y) & length(x)%%2==0){ res <- x+y } else if(length(x)==length(y) & length(x)%%2==1){ res <- abs(x-y) } else print("Lengths are not same") res } vec1 <- 1:10 vec2 <- 11:20 vec3 <- 1:9 vec4 <- 10:18 fun1(vec3,vec4) fun1(vec1,vec2) fun1(vec1,vec3)
A.K. Hi, I'm new on this forum, and I need some help with for loop. I have to write loop that will add two vectors, if they are even. Even if they are odd, it must subtract the two vectors. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.