Introduction to R
8. Logical operators and functions
Besides the standard format of mathematical notation common to many software (addition is +, subtraction is -, multiplication is *, division is /, and raising to a power is ^), the following list of logical operators is useful.
Operator |
Meaning |
== |
Equal to (note it is two equals signs with not space) |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
& |
Boolean “and” |
Vertical line “|” |
Boolean “or” |
Some other useful mathematical functions are:
Function |
Meaning |
max(x) |
Minimum value |
min(x) |
Not equal to |
sqrt(x) |
Square root |
ln(x) |
Natural logarithm |
exp(x) |
e raised to the power of x |
log(x) |
Natural logarithm |
log10(x) |
Logarithm base 10 |
abs(x) |
Absolute value |
round(x) |
Round to the nearest whole number |
sum(x,y,z) |
Sum of x, y and z |
Note that “x” (or y or z) in the above table could be a number or a variable in the dataframe, for example try max(data$age) to find the maximum age in the dataset. They can also be combined. For example, finding the square root of the sum of the ages would be:
sqrt(sum(data$age))
and should return a result of 37.16181