jeudi 15 mars 2018

Functions return number of times a scalar in a vector.

I have to create a function

n_entries(v, x) 

which, given a vector v and a scalar x and returns the number of times that x occurs in v.

Here is what I have so far:

n_entries = function (v,x) {
if(x %in% v) {
    return(count(x))
  }
}

Example: n_entries(1:10, 1) should equal 1 and n_entries(c("a", "a", "b", "c"), "a") should equal 2. Thanks guys.

Aucun commentaire:

Enregistrer un commentaire