How do I create the following function in R?
f(1)=1 f(2)=2 f(3)=3 f(4)=1 f(5)=2 f(6)=3 f(7)=1 f(8)=2 f(9)=3 and so on...
I have tried to use different loops, but have not been able to do the job.
I don't know R but it can be done with Mod3 easily:
function modThree(x) { var mod = x%3; if(mod == 0) return 3; return mod; } modThree = function() {, get rid of var (don't have to declare things) and wrap your returns in parens return(3) and return(mod), and you've got a valid R function.modThree <- function (x) { mod = x%%3 ;ifelse(mod == 0, 3 , mod) } and then it''ll work