Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help using R , thank you The function `sqrt()` provides the square root o

ID: 3745057 • Letter: P

Question

Please help using R , thank you

The function `sqrt()` provides the square root of any number. However, it can't provide any square root of negative number. We want to create our own function to provide a message for negative number.
    a) Create a new `R` function `getRoot()` that will provide square root of any number. If the number is negative it should return 'not possible'. Demonstrate your function such that it produces the following outputs. `getRoot(4) = 2, getRoot(-4) = 'not possible'`
    b) Does your function produce expected results for vector input as well? For example does it give the following result? Explain why or why not. `getRoot(c(4,-4, 9, -16))=2 'not possible' 3 'not possible'`
    c) If your function does not work as expected, how can you make it work properly?

Please help using R , thank you

Explanation / Answer

a) The function definition is below

getRoot <- function(input) {

final=''
for(x in input) {
if(x < 0){
result = 'Not possible'
final = paste(final,result,'')
} else {
result = sqrt(x)
final = paste(final,result,'')
}
}
final
}

print(getRoot(c(4,-4, 9, -16)))

b) Yes the function supports vector inputs as well

c) Yes it works properly, nothing else needs to be done

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote