y^2[1] 49
y^3[1] 343
Now that you’re comfortable with the four basic arithmetic operators and how R decides which operation to do first, we can extend your toolkit with a few more operators and functions that come up often in real analyses. You don’t need to memorize all of these today, treat this page as a reference you can come back to.
We use the caret character, ^, to specify exponents. The following code will square and cube the value of y.
Do: run the code, then predict the output of the next chunk before revealing it.
The sqrt() function returns the square root of its argument (i.e., the value we specify inside the parentheses). The following code calculates the square root of y.
For other roots, we can use fractional exponents.
R also includes many special functions for the most common mathematical operations. Rather than trying to memorize every one of these right now, skim the code and its comments once, then use it as a reference whenever you need one of these operations later.
[1] 1.94591
[1] 0.845098
[1] 2.807355
[1] 12.18249
[1] 2
[1] -3.142
[1] -4
[1] -3
[1] 3.14159
Note that log(y) calculates the natural logarithm of y, \(\ln(y)\). If you want the “ordinary” base-10 log, you need to use the log10() function.
age that takes the value of your age in whole years.age object you created above to create a second object called weeks that takes the value of your age in whole weeks.
age contains 52 whole weeks).weeks.%%, to check whether weeks is evenly divisible by 4.Which function would you use to…
round() / %% / abs())floor() / ceiling() / round())sqrt() / abs() / exp())Try each candidate function in the editor to verify your answer if you’re unsure.