Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

R function generating incorrect results

$
0
0

I am trying to get better with functions in R and I was working on a function to pull out every odd value from 100 to 500 that was divisible by 3. I got close with the function below. It keeps returning all of the values correctly but it also includes the first number in the sequence (101) when it should not. Any help would be greatly appreciated. The code I wrote is as follows:

Test=function(n){
  if(n>100){
   s=seq(from=101,to=n,by=2)
   p=c()
   for(i in seq(from=101,to=n,by=2)){
    if(any(s==i)){
      p=c(p,i)
      s=c(s[(s%%3)==0],i)
     }}
   return (p)}else{
    stop
    }}
  Test(500)

Viewing all articles
Browse latest Browse all 201894

Trending Articles