Question

Question is two parts In R(Programming language). Define and write a function which takes one number...

Question is two parts

In R(Programming language).

Define and write a function which takes one number as input and detects whether the number is even. Return TRUE if it is even, and FALSE otherwise. You need to first detect if the input is an integer; if not, return NA.

  • Note 1: TRUE and FALSE are logical values, not strings!
  • Note 2: No need to get user input from the console. Test your function using your_function_name(1), your_function_name(2), and your_function_name(1.5).
  • Hint: What is the definition of even numbers? In R, you can get the remainder after division using the %% operator (a.k.a. the modulus operator). Example:
5 %% 3 # get the remainder after dividing 5 by 3
## [1] 2

2.2. Write a for loop to get the sum of all the even numbers from -50 to 50. You have to use the function you wrote from the above task.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#2.1
isEven <- function(n){
  if(n %% 2 == 0){#If n leaves remainder 0 divided by 2
    return(TRUE)#n is even
  }
  return(FALSE)#n is odd
}

#Test abpve function
print(isEven(4))

#2.2
sum <- 0
for(i in -50:50){#Loop from -50 to 50
  if(isEven(i)){#If i is even
    sum = sum + i#Add i to the sum
  }
}

print(sum)

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Question is two parts In R(Programming language). Define and write a function which takes one number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints...

    C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints out the answer based on arithmetic. Assume that there are no overflow, underflow and division by zero cases. Your function should be named calculator Your function takes three input parameter: two double numbers and one char operator Your function does not return anything Your function prints answer in the format specified below Your function should set precision point to 2 Note: You must use...

  • Write a function called isEven() that uses the remainder (modulus) operator (%) to determine when an...

    Write a function called isEven() that uses the remainder (modulus) operator (%) to determine when an integer is even. The function should take an integer argument called myNumber passed from within the for loop and return true if the integer is even and false otherwise. Your program should determine what numbers are even and odd from 1 to 20 using the isEven() function. Your counter variable in the for loop should be called num.

  • Using the programming language Java or C++, Write a function that takes an integer as input....

    Using the programming language Java or C++, Write a function that takes an integer as input. Return true if this integer is a palindrome integer; false otherwise;

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • Java Write a Simple Program This question is about following the directions below. Failure to do...

    Java Write a Simple Program This question is about following the directions below. Failure to do so, results in lost credit. Write the definition of a method that takes in an integer number, and returns true if the number is prime, otherwise, returns false. A prime number is only divisible by itself and 1. Do not write the code for main here. The code must follow these steps in the order shown: 1. Name the method isPrime, and make it...

  • #Write a function called check_formula. The check_formula #function should take as input one parameter, a string....

    #Write a function called check_formula. The check_formula #function should take as input one parameter, a string. It #should return True if the string holds a correctly #formatted arithmetic integer formula according to the rules #below, or False if it does not. # #For this problem, here are the rules that define a #correctly-formatted arithmetic string: # # - The only characters in the string should be digits or the five arithmetic operators: +, -, *, /, and =. Any other...

  • Write a function is_mirror(s) that takes as input a string s and returns True if s...

    Write a function is_mirror(s) that takes as input a string s and returns True if s is a mirrored string (i.e., a string that could have been produced by your mirror function) and False otherwise. Examples: >>> is_mirror('baconnocab') result: True >>> is_mirror('baconnoca') result: False Warning Your function should return a boolean value – either True or False, without any quotes around them. If you see quotes around the result when you make the calls above from the console, you must...

  • Write a function in python that takes a set A and a relation R(x, y) on...

    Write a function in python that takes a set A and a relation R(x, y) on A (as a python function such that R(x, y) returns true if and only if the relation xRy holds), and returns True if and only if the relation R is reflexive. Here is the function signature you need to use. def is reflexive(A, R): You can test your code as follows. s = {1,2,3} def y(x, y): return x == y def n(x, y):...

  • write a function which takes a string argument and a character argument. It should return a...

    write a function which takes a string argument and a character argument. It should return a truth value (int 0 or 1), 0 if the string does not contain the character, and 1 if the string does contain the character. Do not use a built-in library for this. Again, call this function and its variables whatever you deem appropriate. The main function of the program should accept a single character followed by Enter. Then, it will read lines until the...

  • LANGUAGE: PYTHON Write a function called: d_polybius(). The function applies the decryption scheme for the polybius...

    LANGUAGE: PYTHON Write a function called: d_polybius(). The function applies the decryption scheme for the polybius cipher scheme above. The start of the function call the get_polybius_square function to get the square as a string. The second scenario when the number of characters is not even, excluding ‘\n’. For instance: “71\n5” is an invalid cipher because there is no way that the last number correspond to a character (we need two numbers). A customized version of Polybius square will be...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT