useR like a calculator
make these command in R to test your skills
> 2+7
[1] 9
> 9+8-5/4*987*8.012*98
[1] -968693.9
> log(100)/log(10)
[1] 2
> tan(3.04*45)
[1] -7.059364
NOTE R ignores spaces, so when you type your math expressions there is
no need to include them. In practice, however, it is helpful to use them because it
makes the commands easier to read and you are less likely to make a mistake
Table 2-1: Some of the Mathematical Operations Available in R
Command/Operation Explanation
+ - / * ( ) Standard math characters to add, subtract, divide, and multiply, as well as
parentheses.
pi The value of pi (π), which is approximately 3.142.
x^y The value of x is raised to the power of y, that is, xy.
sqrt(x) The square root of x.
abs(x) The absolute value of x.
factorial(x) The factorial of x.
log(x, base = n) The logarithm of x using base = n (natural log if none specified).
log10(x)
log2(x)
Logarithms of x to the base of 10 or 2.
exp(x) The exponent of x.
cos(x)
sin(x)
tan(x)
acos(x)
asin(x)
atan(x)
Trigonometric functions for cosine, sine, tangent, arccosine, arcsine, and
arctangent, respectively. In radians.
Some of the mathematical operators can be typed in by themselves—for example, + - * ^ but others
require one or more additional instructions. The log() command, for example, requires one or two
instructions, the first being the number you want to evaluate and the second being the base of the log
you require. If you type in only a single instruction, R assumes that you require the natural log of the
value you specified. In the following activity, you have the opportunity to try out some of the math
and gain a feel for typing commands into the R console window.
1. Type in the following math command using the value of π:
> pi * 2^3 – sqrt(4)
2. Now try using the abs() command with some math:
> abs(12-17*2/3-9)
3. Now type a simple factorial:
> factorial(4)
4. Next, try typing the following logarithms (all give the same answer because they are different forms
of the same thing):
> log(2, 10)
> log(2, base = 10)
> log10(2)
5. Now type in a natural log:
> log(2)
6. Follow up by typing the exponent:
> exp(0.6931472)
7. Type in a logarithm again:
> log10(2)
8. Reverse the logarithm like so:
> 10^0.30103
9. Now try some trigonometry:
> sin(45 * pi / 180)
10. Finally, try reversing the trigonometry from step 9:
asin(0.7071068) * 180 / pi
No comments:
Post a Comment