Doing mathematics with R: Introduction
Arithmetic with numbers
As a first introduction to R and RStudio you can calculate with numbers
Task 1
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).
Advice 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.
Unlock full access