Question

In: Computer Science

Please answer the following by coding in R with comments ! Thank you!!! Evaluation of a...

Please answer the following by coding in R with comments ! Thank you!!!

Evaluation of a square root is achieved using the sqrt() function, but a warning will be issued when the argument is negative. Consider the following code which is designed to test whether a given value is positive before checking whether the square root of the value is less than 5.

testValue <-7

(testValue > 0) & (sqrt(testValue) < 5)

## [1] TRUE

testValue <--7

(testValue > 0) & (sqrt(testValue) < 5)

## Warning in sqrt(testValue): NaNs produced

## [1] FALSE

Modify the code so that it continues to give the correct answer but without the warning.

Solutions

Expert Solution

Let's start with understanding the syntax of sqrt() function in R programming language, the syntax is shown below -

sqrt(numeric_expression)   # Return type is integer

where, numeric_expression can be a positive or negative value.

  • When it is a positive value, the sqrt function returns the square root of given value.
  • When it is a negative value, the sqrt function returns NaN (Not a Number).

Keeping this in mind, let's modify the code to get rid of warning.

Modified code :

testValue <-7

if (testValue > 0){   # when testValue>0
if(sqrt(testValue) < 5)
{
TRUE # when testValue>0 and sqrt(testValue)<5
}
else
{
FALSE # when testValue>0 but sqrt(testValue)>5
}
} else {
FALSE # when testValue<0
}

## [1] TRUE

testValue <--7

if (testValue > 0){ # when testValue>0
if(sqrt(testValue) < 5)
{
TRUE # when testValue>0 and sqrt(testValue)<5
}
else
{
FALSE # when testValue>0 but sqrt(testValue)>5
}
} else {
FALSE # when testValue<0
}

## [1] FALSE

CONCLUSION :

Using if-else conditional statements, it is easy to get the desired output for various test conditions. This modified code uses nested if-else statements to check each possible outcome of the given condition and prints the valid output.


Related Solutions

Please answer the following by coding in R with comments ! Thank you!!! Evaluation of a...
Please answer the following by coding in R with comments ! Thank you!!! Evaluation of a square root is achieved using the sqrt() function, but a warning will be issued when the argument is negative. Consider the following code which is designed to test whether a given value is positive before checking whether the square root of the value is less than 5. testValue <-7 (testValue > 0) & (sqrt(testValue) < 5) ## [1] TRUE testValue <--7 (testValue > 0)...
Please write a complete C coding program (NOT C++) that has the following: (including comments) -...
Please write a complete C coding program (NOT C++) that has the following: (including comments) - declares two local integers x and y - defines a global structure containing two pointers (xptr, yptr) and an integer (z) - declares a variable (mst) by the type of previous structure - requests the values of x and y from the user using only one scanf statement - sets the first pointer in the struct to point to x - sets the second...
Please answer everything in R programming language. Show the code to me as well. Thank You...
Please answer everything in R programming language. Show the code to me as well. Thank You 1. Problem Open dataset stat500. Package: faraway. Use R (a) Calculate the correlation matrix. 10. (b) Plot total vs hw, to see how strong the relationship. (c) Build a simple linear regression: total regressed against midterm. Print model output. (d) Calculate directly the coefficients as in (3) (e) Calculate the Residual standard error s, as in (4). (f) Calculate the standard error of beta_1,...
Please give comments, thank you. In economics, the principle of absolute advantage refers to the ability...
Please give comments, thank you. In economics, the principle of absolute advantage refers to the ability of a party to produce a greater quantity of a good, product, or service than competitors, using the same amount of resources, and The law or principle of comparative advantage holds that under free trade, an agent will produce more of and consume less of a good for which they have a comparative advantage. The more good and services that a country can product...
PLEASE ANSWER J&K :) THANK YOU! 1. Q Corporation and R Inc. are two companies with...
PLEASE ANSWER J&K :) THANK YOU! 1. Q Corporation and R Inc. are two companies with very similar characteristics. The only difference between the two companies is that Q Corp. is an unlevered firm, and R Inc. is a levered firm with debt of $5 million and cost of debt of 10%. Both companies have earnings before interest and taxes (EBIT) of $2 million and a marginal corporate tax rate of 40%. Q Corp. has a cost of capital of...
PLEASE ANSWER J&K :) THANK YOU! 1. Q Corporation and R Inc. are two companies with...
PLEASE ANSWER J&K :) THANK YOU! 1. Q Corporation and R Inc. are two companies with very similar characteristics. The only difference between the two companies is that Q Corp. is an unlevered firm, and R Inc. is a levered firm with debt of $5 million and cost of debt of 10%. Both companies have earnings before interest and taxes (EBIT) of $2 million and a marginal corporate tax rate of 40%. Q Corp. has a cost of capital of...
Please answer all the questions. Thank you Use the following data to answer the questions below:...
Please answer all the questions. Thank you Use the following data to answer the questions below: Column 1 Column 2 Column 3 Y in $ C in $ 0 500 500 850 1,000 1,200 1,500 1,550 2,000 1,900 2,500 2,250 3,000 2,600 What is mpc and mps? Compute mpc and mps. Assume investment equals $ 100, government spending equals $ 75, exports equal $ 50 and imports equal $ 35. Compute the aggregate expenditure in column 3. Draw a graph...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
I need to get this answer rewritten Thank you - -External Factor Evaluation - - Opportunities...
I need to get this answer rewritten Thank you - -External Factor Evaluation - - Opportunities Expansion strategy It can be associated to product diversity or entering into new markets. Expansion strategy  play a big role for the development of the company. This is the opportunity for adidas to expand its product lines to beat its competition. The nature of this strategy is very important because it would provide opportunity to adidas to enter into a new product line and become...
please answer the following question on the bottom. thank you. 4. Why is the mix of...
please answer the following question on the bottom. thank you. 4. Why is the mix of output produced in competitive markets more desirable than that in monopolistically competitive markets? 5. Prior to 1982, AT&T kept local phone rates low by subsidizing them from long-distance profits. Was such cross-subsidization in the public interest? Explain. 6. Why is there resistance to (a) local phone companies providing video and data services and (b) mergers of local cable and telephone companies?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT