tusharkhillare64

tusharkhillare64

Lv2
0 Followers
0 Following
1 Helped

ANSWERS

Published12

Subjects

History1Algebra1Engineering1Computer Science1Calculus1Biology2Statistics1Chemistry4
Answer: the antiderivative of a function f (t) is a function F (t) such that F...
Answer: Step-by-step explanation: the mathematical formula of Henryā€™s law is: ...
Answer: To solve your problem, we need to use Henryā€™s law, which states that t...
Answer: the Iranian Revolution of 1979, and the perspectives of the Iranian pu...
Answer: a truss is a structure that ā€œconsists of two-force members only, where...
Answer: Step-by-step explanation: To calculate the solubility of AuCl in pure ...
Answer: Data plots and projects are a broad topic that can cover many aspects ...
Answer:Itā€™s difficult to say which country is more powerful as it depends on v...

Part A (Functions):Ā  we can write our own functions; this is a good thing for when we need to do the same thing multiple times in a program, for improving code readability, and for reusing code between projects.

Take the following example, the divide_then_add() function Iā€™ve defined below. This function is defined using the keyword function. Immediately following function is a list of the arguments separated by commas, which consist of x and y in this case. The curly brackets { and } surround the code that will be run when the function is run, whatā€™s called the body of the function. Finally, the function is saved in the variable divide_then_add, the name of the function. Once you run this, you can do e.g. divide_then_add(2, 3) to run the function.

# Divides x by y, and then adds x and y to the result divide_then_add <- function (x, y) { (x/y) + x + y

}

The code in the body of a function can be any code and any amount of code (not just a single line), and the result of the last line of code in the body is what the function returns. Itā€™s also possible to create variables inside a function, but these variables are removed once the function is finished running.

# Multiply x by y, store in w, and add z multiply_add <- function (x, y, z) {

w <- x*y

w+z

}

Do the following.

1. Write a function to compute the mean of a numeric vector. Name your function sa6.mean. (You donā€™t need to check if the vector is numeric or has at least two elements.)

2. Write a function to compute a z-score from an observation, a mean, and a standard deviation. You should name your function sa6.z.score, and the first argument should be the observation, the second argument the mean, and the third argument the standard deviation. Use the z-formula from class, š‘§ = š‘„āˆ’ š‘  š‘„ , where š‘„ Ģ„ is the sample mean and š‘  the sample standard deviation.

Ā 

Ā 

Ā 

Ā 

Ā 


Part B (if...else statements): R programs can also be written so that they run different pieces of code, depending on whether certain conditions are set or not. The keywords if introduces a conditional, where it checks if cond is true, and if so, it runs the block of code between the first set of curly brackets. You can also optionally use the else keyword, to say what should happen if cond is false.

if (cond) {

# code to run if cond is true

} else {

# code to run otherwise

}

The following will print "More than five rows!" if the number of rows of df is five or more, and will print "Not more than five :(" if there are fewer than five rows.

if (nrow(df) >= 5) {

print("More than five rows!")

} else {

print("Not more than five :(")

}



The condition can be of arbitrary complexity by using logical operators like & and |, just like the conditions we write when we use subset().

Do the following using the built-in mtcars dataset. You can access it by typing mtcars.

1. Write an if...else statement to do the following.

ā€¢ If the median miles per gallon (mpg) is 20 or more, or the mean horsepower (hp) is 120 or more, print the standard deviation of the engine displacement (disp) using print().

ā€¢ Otherwise, calculate Pearsonā€™s š‘Ÿ for the correlation between weight (wt) and miles per gallon and print it using print().

2. Write an if statement to do the following. If the ratio of my_hp to my_mpg is greater than 5, print that ratio. Otherwise, do nothing. (You can find the ratio by dividing the first number by the second.)

Ā 

Ā 

Ā 

Ā 

Ā 

Part C (for loops): The last control structure weā€™ll practice is a for loop. In a for loop, the program loops through the elements of a vector, and then runs a block of code each

2 time. In the following loop, the program goes through the vector containing the numbers 1 through 15 using the variable i, and prints the result of i+i.

for (i in 1:15) {

print(i+i)

}



Itā€™s possible to loop through vectors of other types, too. For instance, the following will loop through the vector names using the variable name, append the string "LINK" to each, and then print that new string if the name is not "Daron". (You can see you can use other control structures like conditionals in loops, too.)

Ā 

names <- c("Aaron", "Baron", "Caron", "Daron", "Faron")

for (name in names) {

if (name != "Faron") {

print(paste(name, "LINK"))

Ā  Ā  Ā  Ā  Ā }

}

One more example: you can use the iterator variable to access other vectors or data frames at the same position. The following will iterate from 1 to 10, get that number element from the mpg vector of mtcars, multiply it by 10, and then print it.

for (i in 1:10) {

tmp <- mtcars$mpg[i] * 10

print(tmp)

}



Do the following:

1. Use rownames(mtcars) to get the vector containing the names of all the cars in the mtcars dataset. Then, loop through this vector using a for loop, and print the name of any car where the name is greater than or equal to 15 characters long. (Use nchar() to check the length of a string.)

2. Create a for loop that does the following:

ā€¢ Uses rownames(mtcars) to get a vector of the names of cars.

ā€¢ Iterates from 1 to as many rows as there are in mtcars (use nrow() to get the number of rows in a data frame).

ā€¢ For each row, prints the name of the car and that carā€™s miles per gallon (use paste() to put these together into one string, like paste(name, mpg)).

Answer: Step-by-step explanation: To write a function in R, you first need to ...
Answer: Living things are organisms that are made up of cells and are capable ...
Answer: A globe is a spherical model of Earth, of some other celestial body, o...
Answer: To make 4 sandwiches using the formula 2Bd + 1Ch -> Bd2Ch, we need ...

Weekly leaderboard

Start filling in the gaps now
Log in