Now that we have R set up, this guide will illustrate R functionality by showing snippets of R code and the output they produce. Each code snippet will consist of 2 parts, an input block showing the actual code I'm running (possibly with some descriptive comments) followed by the output produced by the code. It is recommended that you keep RStudio open and play around with the commands yourself as you read along.
In this part, we will explore R's ability to perform basic computations. As we saw in part 1, typing an arbitrary mathematical expression into the console yields the result of the calculation, just as we'd expect when using a calculator. R accepts all the basic math symbols you'd expect:
In [1]:
# Use + for addition
4+5
Out[1]:
In [2]:
# Use - for subtraction
5-10
Out[2]:
In [3]:
# Use * for multiplication
8*4
Out[3]:
In [4]:
# Use / for division
100/3
Out[4]:
In [5]:
# Use ^ for exponentiation
2^4
Out[5]:
Math expressions in R follow the normal order of operations so * and / are executed before + and -, and ^ is executed before multiplication and division.
In [6]:
# These operations are executed in reverse order of appearance.
2+3*5^2
Out[6]:
You can use parentheses in your math expressions to ensure that operations are carried out on the correct order. Operations within parentheses are carried out before operations that are external to the parentheses, just like you'd expect.
In [7]:
# Now the addition comes first and the exponentiation comes last.
((2+3) * 5 )^2
Out[7]:
If you're new to programming, you may not be familiar with the modulus operator, but it is another common math symbol that returns the remainder you'd get when dividing two numbers. Use two percentage signs to take the modulus.
In [8]:
# Use %% for modulus
100 %% 30
Out[8]:
Beyond symbolic operators, R contains a wide variety of named math functions. Here is brief summary of a few of the most common math functions you might want to access:
In [9]:
log(2.7182) # log() takes the natural logarithm of its argument
Out[9]:
In [10]:
exp(10) # exp() raises e to the power of its argument
Out[10]:
In [11]:
# If you ever need the constant e you can use:
exp(1)
Out[11]:
In [12]:
log10(100) # log10() takes the log base 10 of a number
Out[12]:
In [13]:
log2(64) # log2() takes the log base 2 of a number
Out[13]:
In [14]:
sqrt(64) # sqrt() returns the square root of its argument
Out[14]:
In [15]:
# round() rounds its argument to the nearest whole number:
round(233.234)
Out[15]:
In [16]:
# Add the "digits" argument to round to specified decimal place:
round(233.234, digits=1)
Out[16]:
In [17]:
# Entering -1 for digits rounds to the 10's place:
round(233.234, digits=-1)
Out[17]:
If a number is equal in distance between the two numbers it could round to, round() will prefer the even number. In other words, R does not always round up when it encounters a 5 in the decimal place you are rounding:
In [18]:
round(2.5) # Rounds down to the even digit 2
round(3.5) # Rounds up to the even digit 4
Out[18]:
Out[18]:
Use the floor() and ceiling() functions to round a number down to or up to the next whole number.
In [19]:
floor(2.8) # floor() always rounds down
Out[19]:
In [20]:
ceiling(2.2) # ceiling() always rounds up
Out[20]:
In [21]:
trunc(2.6) # trunc() rounds in the direction of zero
trunc(-2.6)
Out[21]:
Out[21]:
In [22]:
abs(-10) # abs() returns the absolute value of the argument
Out[22]:
In [23]:
pi # pi returns the constant pi
Out[23]:
Common trigonometric functions are also built-in. The trig functions assume your argument is expressed in terms of radians, not degrees.
In [24]:
cos(0) # Cosine
sin(pi/2) # Sine
tan(pi/4) # Tangent
Out[24]:
Out[24]:
Out[24]:
In [25]:
acos(1) # Inverse Cosine
asin(1) # Inverse Sine
atan(1) # Inverse Tangent
Out[25]:
Out[25]:
Out[25]:
Any time you need to perform a common mathematical operation in R, chances are there is a function for it built into R or available through an R package download (we'll discuss packages later.). When in doubt, try searching Google. Helpful blog posts and answers posted on programming sites like stackoverflow can often save you a lot time and help you learn better ways of doing things.
Next time: Introduction to R Part 3: Atomic Data Types
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.