Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R programming help on writing functions

I am trying to write a code that shows a vector who will only show elements who 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]
        }
    }
}

It keeps giving me error. Can anyone help?

I'm still very new to writing codes and always have trouble doing so.

Any suggestions to improve would be greatly appreciated.

Thank you.

I made some changes and another warning message occurred.

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
like image 884
Faithhhhhh Avatar asked Nov 27 '25 05:11

Faithhhhhh


2 Answers

You want the "and" operator & instead of "or" |. Also, there's no need to loop, you can take advantage of R's 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 it without writing a 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
like image 146
Rich Scriven Avatar answered Nov 29 '25 19:11

Rich Scriven


There is already a vectorized solution on the table and it is far preferable but I thought the task of building a loop that actually runs would also be useful. You initialized a loop-length value, k, but never used it, rather looping on 1:x. That's the first basic error (beyond than using "|" rather than "&" which has already been pointed out). And another error was not keeping an accumulator of valid results which could be returned after running the loop. And the third logical error was testing values of the loop-index rather than values of 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
like image 38
IRTFM Avatar answered Nov 29 '25 19:11

IRTFM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!