Question

In: Computer Science

R programming language A) Create a function that accepts two arguments, an integer and a vector...

R programming language

A) Create a function that accepts two arguments, an integer and a vector of integers. It returns the count of the number of occurrences of the integer in the input vector.

1]Input: num_count <-function ???

2]Input: num_count(2,c(1,1,2,2,3,3))

2] Output: 2

3] Input: num_count(1,c(1,1,2,2,3,1,4,5,5,2,2,1,3))

3] Output : 4

B) Create a function that accepts 3 integer values and returns their sum. However, if an integer value is evenly divisible by 3, then it does not count towards the sum. Return zero if all numbers are evenly divisible by 3. Hint: You may want to use the append() function.

1] Input: summer <- function???

2] Input: summer(7,2,3)

2] Output: 9

3] Input: summer(3,6,9)

3] Output:0

4] Input: summer(9,11,12)

4] Output: 11

C)Create a function that will return TRUE if an input integer is prime. Otherwise, return FALSE. You may want to look into the any() function

1]Input: prime_check <- function ???

2]Input: prime_check(2)

2]Output:  TRUE

3] Input: prime_check(5)

3] Output: TRUE

4] Input: prime_check(4)

4] Output: FALSE

5]Input: prime_check (237)

5] Output: FALSE

6] Input: prime_check(131)

6] Output: TRUE

Solutions

Expert Solution

#function to calculate the count of given number in a vector
num_count <- function(num, v){
#check the vaues equal to num in the vector
#calculate the sum
sum(v == num)
}
#call the function
num_count(2, c(1, 1, 2, 2, 3, 3))

#function to calculate the sum of number which are not divisible by 3
summer <- function(num1, num2, num3){
#create a vector with the three numbers
vals <- c(num1, num2, num3)
#calculate the sum if they are not divisible by 3
sum(vals[vals %% 3 != 0])
}
#call the function
summer(3, 6, 9)

#function to calculate whether a given number is prime number
prime_check <- function(num){
#if the number is less than 2 reutrn false
if(num < 2)
{
return(FALSE)
}
#if the number is true return true
if(num == 2)
{
return(TRUE)
}
#create a vector from 2 to num - 1
vals <- seq(2, num - 1)
#check if there are any factors
#if there are no factors then it is a prime number
!any(vals %% num == 0)
}
#call the function
prime_check(7)


Related Solutions

In c++ Write a function that: accepts a string and an integer as it's only arguments...
In c++ Write a function that: accepts a string and an integer as it's only arguments uses the argument to open a file for writing writes the integer to the file if it fails to open the file, returns -1 if it succeeds in opening the file, returns 0 does not interact with the user in any way does not use global variables
PWrite a VHDL function that accepts a vector of arbitrary length and integer that specifies the...
PWrite a VHDL function that accepts a vector of arbitrary length and integer that specifies the number of bits the input vector to be rotated to the left and returns another vector. For instance functions accepts two inputs: 0101011 and 3 and it returns 1011010.
R Programming: create a vector for 1 to 31 and count the number of even and...
R Programming: create a vector for 1 to 31 and count the number of even and odds using ifelse()
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
In a program, write a function that accepts two arguments: a list, and a number n....
In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. The program should ask for a list of numbers from the user as well as a value (a, b, c)--> inputs from user. After that, each number in that list should be compared to that value (a or b or...
This is R language Create a vector that contains the message part of each log entry...
This is R language Create a vector that contains the message part of each log entry in logsin uppercase letters. In your solution, you might want to use the function toupper(). Simply print this vector to the console. # Return vector with uppercase version of message elements in log entries # Apply extract_caps function on logs
Write a function that accepts two arguments (say a and b) by value and one argument...
Write a function that accepts two arguments (say a and b) by value and one argument (say c) by reference. Then it calculates the multiplication of all the numbers between a and b (inclusive) and saves the results in c. The function does not return any value.
The C function funsum below takes three arguments -- an integer val and two function pointers...
The C function funsum below takes three arguments -- an integer val and two function pointers to functions f and g, respectively. f and g both take an integer argument and return an integer result. The function funsum applies f to val, applies g to val, and returns the sum of the two results. Translate to RISC-V following RISC-V register conventions. Note that you do not know, nor do you need to know, what f and g do, nor do...
The C function funsum below takes three arguments -- an integer val and two function pointers...
The C function funsum below takes three arguments -- an integer val and two function pointers to functions f and g, respectively. f and g both take an integer argument and return an integer result. The function funsum applies f to val, applies g to val, and returns the sum of the two results. Translate to RISC-V following RISC-V register conventions. Note that you do not know, nor do you need to know, what f and g do, nor do...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT