Example 1
\[
y = \ln(x^2)
\]
ln_sq <- function(z){
y <- log(z^2)
return(y)
}
ln_sq(34)
[1] 7.052721
Example 2
\[
f(x) = \left\{\begin{array}{cc}
x^3 & x<0\ \\
x^2 + 5 & \mathrm{otherwise}
\end{array} \right.
\]
secondfunction <- function(x){
if(x<0){
y = x^3
} else{
y = x^2 +5
}
return(y)
}
secondfunction(-1)
[1] -1
secondfunction(1)
[1] 6
Example 3
\[
f(x,y) = \left\{\begin{array}{cc}
x^3 e^y & x<0\ \\
x^2 + 5 + \ln(y) & \mathrm{otherwise}
\end{array} \right.
\]
this <- function(x, y) {
if(y < 0) {
stop("it is negative")
}
if(x < 0) {
z <- x^3 * exp(y)
} else {
z <- x^2 + 5 + log(y)
}
return(z)
}
this(2,1)
[1] 9
this(-2,1)
[1] -21.74625
this(2,-1)
Error in this(2, -1) : it is negative
Example 4
\[
z = \frac{x-\bar y}{\sqrt{s^2_{y}/n_y}}
\]
zscore <- function(x,y){
(x-mean(y))/(sqrt(var(y)/length(y)))
}
zscore(5,1:5)
[1] 2.828427
LS0tCnRpdGxlOiAiQ2xhc3MgNiIKYXV0aG9yOiAiSXNhYWMgUXVpbnRhbmlsbGEgU2FsaW5hcyIKZGF0ZTogImByIGZvcm1hdChTeXMudGltZSgpLCclbS0lZC0lWScpYCIKb3V0cHV0OiAKICBodG1sX25vdGVib29rOgogICAgdG9jOiB0cnVlCiAgICB0b2NfZmxvYXQ6IHRydWUKICAgIGNvZGVfZm9sZGluZzogaGlkZQoKLS0tCgojIyBFeGFtcGxlIDEKCiQkCnkgPSBcbG4oeF4yKQokJAoKYGBge3J9CmxuX3NxIDwtIGZ1bmN0aW9uKHopewogIHkgPC0gbG9nKHpeMikKICByZXR1cm4oeSkKfQpsbl9zcSgzNCkKYGBgCgojIyBFeGFtcGxlIDIKCiQkCmYoeCkgPSBcbGVmdFx7XGJlZ2lue2FycmF5fXtjY30KeF4zICYgIHg8MFwgXFwKeF4yICsgNSAmIFxtYXRocm17b3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