a. (10 pts) Write a function named absoluteValue that takes a numeric argument n and returns its absolute value. You will return n if n is positive or 0, return –n if n is negative. You are not...

1 answer below »
write functions in R that follow the requirement in attached


a. (10 pts) Write a function named absoluteValue that takes a numeric argument n and returns its absolute value. You will return n if n is positive or 0, return –n if n is negative. You are not allowed to use built-in functions. Write another function named distance that takes two vector v and w as arguments and returns |?1 − ?1 | + |?2 − ?2 | + ⋯ + |?? − ?? |, where k is the length of v and w. You are not allowed to use built-in functions for computation. You should use your absoluteValue function to compute absolute value. You should firstly check whether v and w are numeric vectors and their lengths are equal. Otherwise, print an appropriate error message and stop execution of the function. Then, using a loop, compute and return their distance. For instance, distance(1:3,4:6) should return 9, distance(c(4,9,6),c(5,7,6)) should return 3 b. . b. (10 pts) Write a function named moveVowelsFront that takes a String argument and returns a new string by moving all vowels in the argument to the front of it. For instance, moveVowelsFront(“Hello”) will return “eoHll”. moveVowelsFront("University of Houston-Downtown") will return "Uieioouooonvrsty f Hstn-Dwntwn". Hint: You can create an empty string s and append all vowels to s in a loop. Then, in another loop you can append all other characters to s and return s at the end.
Answered Same DayAug 15, 2021

Answer To: a. (10 pts) Write a function named absoluteValue that takes a numeric argument n and returns its...

Pritam Kumar answered on Aug 16 2021
145 Votes
absoluteValue <- function (n) {
if (n==o) {
return (n)
}
else if (n>0) {
return (
n)
}
else {
return (-n)
}
}
absoluteValue(-3)
absoluteValue(8)
absoluteValue(0)
distance <- function (v,w) {
count = 0
distance = 0
if (length(v) != length(w)) {
print("lengths of both vectors have to be same")
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here