[1] 1701
Functions writing functions (solution)
1. Copy the following function (which converts weights in pounds to weights in grams) into your assignment and replace the ________
with the variable names for the input and output.
<- function(________) {
convert_pounds_to_grams = 453.6 * pounds
grams return(________)
}
Use the function to calculate how many grams there are in 3.75 pounds.
2. Copy the following function (which converts temperatures in Fahrenheit to temperatures in Celsius) into your assignment and replace the ________
with the needed commands and variable names so that the function returns the calculated value for Celsius.
<- ________(________) {
convert_fahrenheit_to_celsius = (fahrenheit - 32) * 5 / 9
celsius ________(________)
}
Use the function to calculate the temperature in Celsius if the temperature in Fahrenheit is 80°F.
3. Write a function named double
that takes a number as input and outputs that number multiplied by 2. Run it with an input of 512.
4. Write a function named prediction
that takes three arguments, x
, a
, and b
, and returns y
using y = a + b * x
(like a prediction from a simple linear model). Run it with x
= 12, a
= 6, and b
= 0.8.
##Code solution for Writing Functions 1.
[1] 26.66667
- Write a function named
double
that takes a number as input and outputs that number multiplied by 2. Run it with an input of 512.
[1] 1024
- Write a function named
prediction
that takes three arguments,a
,b
, andx
, and returnsy
usingy = a + b * x
(like a prediction from a simple linear model). Run it witha
= 6,b
= 0.8, andx
= 12.
[1] 15.6