Create a function that accepts two arguments, an integer and a vector of integers. It returns TRUE if the integer is present in the vector, otherwise it returns FALSE.

1.8K    Asked by Nabhashahin in Data Science , Asked on Jan 2, 2020
Answered by Nabha shahin

Let us create a function which returns TRUE and FALSE based on the condition

num_check <- function(num,v){

    for (item in v){

        if (item == num){

            return(TRUE)

        }

    }

    return(FALSE)

}

Now let us check whether it is working or not.

num_check(2,c(1,2,3))

Output

TRUE

num_check(2,c(1,4,5))

Output

FALSE



Your Answer

Interviews

Parent Categories