R help writing functions

I am trying to write code that shows a vector that will only show elements that are not divisible by 2,3 or 7.

function2 <- function(x){
    k <- length(x)
    for(i in 1:x){
        if(i%%2!=0 | i%%3!=0 | i%%7!=0){
            return x[i]
        }
    }
}

      

He keeps giving me error. Can anyone please help?

I am still very new to coding and always have problems with this.

Any suggestions for improvement would be greatly appreciated.

Thank.

I made some changes and another warning message came up.

function2 <- function(x) {
    k <- length(x)
    for(i in 1:x){
        if(i%%2!=0 | i%%3!=0 | i%%7!=0) {
            show(x[i])
        } else {
            i <- i+1
        }
        print(x[i])
    }
}

x <- 1:100
function2(1:100)
## [1] 1
## [1] 1
## Warning message:
## In 1:x : numerical expression has 100 elements: only the first used

> foo <- function(x) {
+     x[(x %%2 != 0) | (x %% 3 != 0) | (x %% 7 != 0)]
+ }
> foo(1:100)
 [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19
[20]  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38
[39]  39  40  41  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58
[58]  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77
[77]  78  79  80  81  82  83  85  86  87  88  89  90  91  92  93  94  95  96  97
[96]  98  99 100

      

+3


source to share


2 answers


You need the and operator &

instead of the or |

. Also, there is no need for a loop, you can use R vectorization.

foo <- function(x) {
    x[(x %% 2 != 0) & (x %% 3 != 0) & (x %% 7 != 0)]
}
foo(1:50)
# [1]  1  5 11 13 17 19 23 25 29 31 37 41 43 47

      



You can also do this without writing the function

x <- 1:50
x[(x %% 2 != 0) & (x %% 3 != 0) & (x %% 7 != 0)]
# [1]  1  5 11 13 17 19 23 25 29 31 37 41 43 47

      

+3


source


There is already a vectorized solution in the table and this is much preferable, but I thought that the task of creating a loop that actually works would be helpful as well. You have initialized a loop length value k

, but never used it, rather a loop on 1:x

. This is the first major mistake (besides usage "|"

, not the "&"

one already pointed out). And another mistake was that the accumulator was not storing valid results that could be returned after starting the cycle. And the third logical error was checking the loop index values, not the values x

:



function2=function(x){
    k=length(x); val <- c()
    for(i in 1:k){    # probably should have used seq_along(x), and skip 'k'
         if(!( x[i]%%2==0 | x[1]%%3==0 | x[i]%%7==0)){  # could have used `"||"`
                       val <- c(val,x[i])            }
                 }
     return (val)     }  # don't need the return(), could just evaluate `val`

 x<-(1:100)
 function2(1:100)
[1]  1  3  5  9 11 13 15 17 19 23 25 27 29 31 33 37 39 41 43 45 47 51 53 55 57 59
[27] 61 65 67 69 71 73 75 79 81 83 85 87 89 93 95 97 99

      

+3


source







All Articles