As a first introduction to R and RStudio you can calculate with numbers
Create an R script (save the file with the .R extension) containing the following lines of code:
# Some calculations with natural numbers and
# conversion from Celsius toFahrenheit
# A. Heck
# 19-11-2018
one <- 1
two <- 2
three <- one + two
cat("The type of the R object", three, "is", typeof(three), "\n")
cat(one, '+', two, '=', three, "\n")
cat('6 / 8 =', 6 / 8, "\n")
cat('8 / 6 =', 8 / 6, "\n")
quotient <- 8 %/% 6; rest <- 8 %% 6;
cat('8 %/% 6 =', quotient, 'en 8 %% 6 =', rest, "\n")
Run this R script and you will get the following output in the console window:
The type of the R object 3 is double
1 + 2 = 3
6 / 8 = 0.75
8 / 6 = 1.333333
8 %/% 6 = 1 en 8 %% 6 = 2
These few lines of code already call for an explanation, but first think about what the output means before reading the explanation (click on the explanation tab at the bottom right of this box).
The first lines are meant as documentation
We can separate R instructions (if they are complete) with an empty line to have a better overview in the program file.
The next three lines show you how to store values in self-chosen names called variables and use those names in further calculations. The next four statements show that you can print multiple things one after the other: texts, in the form of a string object, are separated with a double quote "
and R separates parts in the result of the cat
statement with a space. The newline escape character \n
is used to force a new line.
Another way to specify a string object is to use the single quote '
, but don't mix the two types of quotes: "gives error' is incorrect, but "Marthe's bike" is a good string with a quote in the middle.
The last two R instructions show calculations with natural numbers: /
is for 'ordinary division', which may or may not return a fraction in the form of a floating-pointed number. Not every fraction can be represented as a finite floating-point number: in this case \(8/6 = 4/3\) is only stored in 16 decimal places. The operator %/%
is for 'divide with remainder' and %%
is the symbol for the modulo operator.
\(\phantom{x}\)
We summarise the basic numerical operations with numbers:
\[\begin{array}{l|c|l}
\textit{Operation} & \textit{Operator} & \textit{Explanation} \\ \hline\hline
\text{addition} & x+y & x\text{ and }y\text{ are integers or floating-point numbers.} \\ \hline
\text{difference} & x-y & x\text{ and }y\text{ are integers or floating-point numbers.} \\ \hline
\text{product} & x*y & x\text{ and }y\text{ are integers or floating-point numbers.} \\ \hline
\text{quotiënt} & x/y & x\text{ and }y\text{ are integers or floating-point numbers..}\\[-2pt]
& & \text{The result is a floating-point number.} \\ \hline
\text{integer division} & x\;\%/\%\;y & x\text{ and }y\text{ are integers or floating-point numbers.}\\[-2pt]
& & \text{The result is the greatest integer less than or}\\
& & \text{equal to the quotiënt.} \\ \hline
\text{remainder in division} & x\;\%\%\;y & x\text{ and }y\text{ are integers.}\\[-2pt]
& & \text{The result is the remainder in division of }x\text{ by }y\text{.} \\ \hline
\text{exponentiation} & x \;\mathtt{\hat{\;}} \;y & x\text{ and }y\text{ are integers or floating-point numbers.} \\ \hline
\text{floating-point conversion} & \mathtt{as.double}(x) & \text{Convert the numerical value of }x\\[-2pt]
& & \text{into a floating-point number.}\\ \hline
\text{integer conversion} & \mathtt{as.integer}(x) & \text{convert the numerical value of }x\text{ to an integer.}\\[-2pt]
& & \text{ Cut off the decimal representation; do not round off.}\\ \hline
\text{absolute value} & \mathtt{abs}(x)& \text{Compute the absolute vale of }x\text{.} \\ \hline
\text{rounding} & \mathtt{round}(x) & \text{Round the numerical value of }x\text{ to the nearest integer.}\\
& \mathtt{round}(x, \text{digits=}n) & \text{Round }x\text{ up to }n\text{ decimals.}\\ \hline
\text{sign} & \mathtt{sign}(x) & \text{The result is } \left\{ \begin{array}{ll}
-1 & \text{if }x \lt 0 \\ 0 & \text{if }x=0 \\ 1 & \text{if }x\gt 0 \end{array} \right. \\ \hline
\text{factorial} & \mathtt{factorial}(n) & n! \end{array}\]
\(\phantom{x}\)
The associated data types for variables are
\[\begin{array}{l|c|l}
\textit{Type} & \textit{Example} & \textit{Explanation} \\ \hline\hline
\text{integer} & 12 & \text{Integer values} \\ \hline
\text{double} & 12.345 & \text{Approximations of real number in a maximum of 16 decimals} \\ \hline
\text{character} & \text{"Hello"} & \text{Collection of characters. A string must always be between quotation marks} \\ \hline
\text{logical} & \text{TRUE} & \text{Outcome of a logical logische test. Two possible values: TRUE of FALSE}\\ \hline
\end{array}\]
You can check the data type of a variable with the typeof
function. You can convert data types with the functions as.double
, as.integer
, and as.character
.
Ensure that the name of a variable in an R program is easy to understand, so that you can quickly pick up the context later and continue working on your program.