Question

In: Computer Science

PLEASE USE R PROGRAMMING LANGUAGE TO ANSWER THESE EXAMPLES. Also please explain any important reasons/tips for...

PLEASE USE R PROGRAMMING LANGUAGE TO ANSWER THESE EXAMPLES. Also please explain any important reasons/tips for how you coded. Thank you!

The Basic Examples:

a) If Statements: Write code that generates a random variable x uniformly between 0 and 1. If x is bigger than 0.6, square it; if x is less than 0.3, set it equal to 5, otherwise set x = 0.

b) Forloops: Write a program that makes a vector of all zeros of length 5.Then write a for loop that fills in this vector with the squares of the integers from 1 to 5. This for loop should iterate over the numbers 1 to 5, square each one, and enter the result in the vector. Show the results in your output/report.

c) Functions: Write a function that takes a number as an input and squares it (so the output is the square of the input number). Rewrite your code from part b) to use your new function, and run the updated code. Do your results match the results you got in part b)?

Solutions

Expert Solution

The program is as below

########
# Answer of part A
########
x = runif(1)
cat('The random value of x is :', x, '\n')
if (x > 0.6 ){
x = x*x
}else if(x < 0.3){
x = 5
}else x = 0
cat('value of x after conditional checking', x, '\n')

########
# Answer of part B
########
# define a vector v having 5 elements each one is 0
v = replicate(5, 0)
cat ('Initial value of vector v is: ', v,'\n')
#print(v)
for ( i in 1:5) {
v[i] = i*i
}
cat ('Final value of vector v is: ', v,'\n')
#print(v)

########
# Answer of part C
########
# User defined function
square.function <- function(a) {
result <- a * a
}
# initialize vector v with 0
v = replicate(5, 0)
for ( i in 1:5) {
v[i] = square.function(i)
}
cat ('Value of vector v using square function is: ', v,'\n')
#print(v)

Sample output:

The random value of x is : 0.2684412 
value of x after conditional checking 5 
Initial value of vector v is:  0 0 0 0 0 
Final value of vector v is:  1 4 9 16 25 
Value of vector v using square function is:  1 4 9 16 25 

The output implies that result of part C is same as result of part B


Related Solutions

Use R programming language to answer and please so show the code as well. A paper...
Use R programming language to answer and please so show the code as well. A paper manufacturer studied the effect of three vat pressures on the strength of one of its products. Three batches of cellulose were selected at random from the inventory. The company made two production runs for each pressure setting from each batch. As a result, each batch produced a total of six production runs. The data follow. Perform the appropriate analysis. Table is below Batch Pressure...
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,...
IMPORTANT!!! Please answer it precisely. It is expected to use academic knowledge and language. Use your...
IMPORTANT!!! Please answer it precisely. It is expected to use academic knowledge and language. Use your own words, do not copy paste. Suppose you are a microbiologist living in late 1800ies and early 1900. You suspect that the microorganism called Mycobacterium leprae might be responsible for the disease called leprosy. You had heard about a researcher called Robert Koch and his postulates, and you would like to follow his approach to test if leprosy is caused by Mycobacterium leprae. Explain...
IMPORTANT!!! Please answer it precisely. It is expected to use academic knowledge and language. Use your...
IMPORTANT!!! Please answer it precisely. It is expected to use academic knowledge and language. Use your own words, do not copy paste. Please explain in detail how the microbiology information about the infectious diseases and their epidemiology, aseptic techniques of microbiology (sterilization, disinfection, antimicrobial techniques and agents) helped you during the COVID-19 pandemic. Please give detailed examples of how you used your microbiology and/or scientific knowledge in helping/informing people in your family and/or your community and/or how different was your...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the Weightlifting Walrus is training for a contest where it will have to lift 1000 kg. Wallace has some weight plates lying around, possibly of different weights, and its goal is to add some of the plates to a bar so that it can train with a weight as close as possible to 1000 kg. In case there exist two such numbers which are equally...
Please use the Java Programming language. This is the introductory course, chapter two. Please only use...
Please use the Java Programming language. This is the introductory course, chapter two. Please only use if/else if, else and while loop. We have not touch base with do and while do(I don't know if while do exist in Java). Create an application that converts number grades to letter grades. Console Welcome to the Letter Grade Converter Enter numerical grade: 90 Letter grade: A Continue? (y/n): y Enter numerical grade: 88 Letter grade: A Continue? (y/n): y Enter numerical grade:...
In the R programming language, we would like to use the data set called iris to...
In the R programming language, we would like to use the data set called iris to build a simple linear regression model to predict Sepal.Length based on Petal.Length. Calculate the least squares regression line to predict Sepal.Length based on Petal.Length. Interpret the slope of the line in the context of the problem. Remember that both variables are measured in centimeters. Plot the regression line in a scatterplot of Sepal.Length vs. Petal.Length. Test H1: ??1 ≠ 0 at ?? = 0.05...
Question 1 Selecting the programming language is important Group of answer choices It allows a programmer...
Question 1 Selecting the programming language is important Group of answer choices It allows a programmer to pick their favorite language It identifies the best language as the best tool for solving a particular problem It enables only certain programmer to communicate None of these Question 2 High level languages include Group of answer choices Basic, Fortran, Cobol, Pascal, C HTML, XML Java, C++ All of these Question 3 A major ethical concern on the Internet Group of answer choices...
Please use C programming to write the code to solve the following problem. Also, please use...
Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...
Please use the Scheme programming language with Dr. Racket to solve a and b below. (Use...
Please use the Scheme programming language with Dr. Racket to solve a and b below. (Use Scheme and not Python) Write before–in–list?, which takes a list and two elements of the list. It should return #t if the second argument appears in the list argument before the third argument: > (before–in–list? '(back in the ussr) 'in 'ussr) #T > (before–in–list? '(back in the ussr) 'the 'back) #F The procedure should also return #f if either of the supposed elements doesn't...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT