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
Make a python code. Write a function named max that accepts two integer values as arguments...
Make a python code. Write a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 7 and 12 are passed as arguments to the function, the function should return 12. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is the greater of the two. Write the program as a loop that...
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-Studio (R Programming Language) 1. How would you create a vector `V` containing the values 0,...
R-Studio (R Programming Language) 1. How would you create a vector `V` containing the values 0, 0.25, 0.5, 0.75, and 1?    ```{r} #insert your code ``` 2. Name the elements of `V`: first, second, middle, fourth, last. Describe two ways of naming elements in `V` ```{r} #insert your code ``` 3. Suppose you keep track of your mileage each time you fill up. At your last 6 fill-ups the mileage was 65311 65624 65908 66219 66499 66821 67145 67447....
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()
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...
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The...
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The function will check each character of the strings in the array and removes the character, regardless of case. If the first letter of the string is removed the next letter must be capitalized. Your function would return the result as one string, where each element of array is separated by comma. E.G. function([“John”,”Hannah”,”Saham”], “h”); // returns ‘Jon,Anna,Saam Use of any built in string function...
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):
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT