library(learnr)
library(knitr)
knitr::opts_chunk$set(echo = TRUE)

First runic conversations

Welcome to this first chapter of the IGoR grimoire. In this chapter, we will gradually:

If necessary, do not hesitate to request one of the quizmasters moving around the room.

But let's start… at the beginning! R is a language used for processing statistical data, the first version of which was published in 2000. First disseminated among researchers, R is now used more and more, especially within national statistical institutes.

R allows you to make statistical computations, to present them in the form of tables and also to draw graphs and geographical maps. The results produced with R can be exported into reports and even be integrated in web applications. Mastering R, the Language of the Runes is a great power ~that comes with great responsibility~!

Unlike other software like SAS, R is a free software: its license is free and allows each user to access, modify or redistribute its source code. The source code is the equivalent of the manual for making the software. It is also cross-platform: this means that it can be run on several operating systems (Windows, Mac, Linux ...)

Quizzes you will defeat

In the adventure of IcaRius, you will sometimes be asked to answer quizzes to help the character continue his quest. Here is a very first one, just to familiarize yourself with the format of the exercise. So it's up to you to respond directly by checking your choice and then pressing the button called Submit answer.

If you need to know how to operate, do not hesitate to ask for one of the quizmasters moving around the room.

quiz(
  question("What does R NOT do?",
    answer("very nice graphics"),
    answer("statistical computations"),
    answer("awesome geographical maps"),
    answer("coffee", correct = TRUE),
    answer("web applications")
  ),
  question('What does "free software" mean?',
    answer("a free software is a wild software, it cannot be found at home"),
    answer("a free software is a software that freely authorizes the use, study, modification and redistribution", correct = TRUE),
    answer("a free software is a software working only with escape key")
  )
)

First steps in the console

You might think that we are going to offer you a video game “console”. Unfortunately, this is not quite the case. In the quest, we will talk about a console to designate a part of the screen where you can practice the Rune Language yourself - in other words, write R yourself and see the result. So it's an R console!

Under these lines is an R console and, like a calculator, it is possible to ask it to perform calculations: type below, in the console, a calculation such as 2 + 2 (that's all even harder than 1 + 1). Then press the button at the top right of the console to execute the code (run code means execute the code). The answer to your calculation will then be displayed below the console.


R fortunately answers 4! In the future we're going to make it do much more complicated things ...

R has its tics, like everything else. Here, its tic of language is to display the value of an object starting with something like [1]. Kind of like someone who always starts sentences with "actually" (or "uh" depending on how precious his language is).

Practice...

Here is a slightly larger console that has some pre-populated instructions, always run them with the button to see how R structures its response to your instructions

3 + 3
3+3
12345678 * 4

The console is a place of dialogue, you command and R does it! Note that you see all the results appear one after the other (for example, there are 3 calculations requested separately here , and R answers you with 3 results): therefore, there is one answer per instruction.

In the way of writing the language of Runes, the number of spaces is irrelevant: 3 + 3, 3+3 are all the same.

Each language has its own way of understanding mathematical operations. For fundamentals, R uses + for addition and - for subtraction (so far so good), but also * for multiplication and / for division.

Try some calculations, freely and on your own:


Put R in order

Create an object

To go further than a simple calculator, R allows you to remember all the calculations you ask it to perform and also to keep the result of these calculations. To navigate this gigantic memory, a little organization is needed!

In R, information can be stored in what we will call objects: to illustrate, an object is like a box on which we can affix a label (a name) and in which we store a content (the result of a calculation for example). Giving names to objects helps you navigate long calculations.

For example, we are going to create an object named n_lives, which will be used to store the number of the hero's lives in the game. Icarius starts its journey with 3 lives, therefore the value we want to give to n_lives is 3. To store the value 3 in the object called n_lives, the runic language has a very particular sign, namely the arrow <-. It is constructed by juxtaposing the sign < with the dash -. The assignment of a value to an object is performed with the arrow <-. For example:

# I create the n_lives object (number of the hero's lives)
# assigning to it the value 3
n_lives <- 3

The above code assigns the value 3 to the n_lives object. You will notice that the arrow used to assign is oriented from right to left: it goes from the value (here 3) to the object that collects the value (here n_lives).

By the way, in R, anything that appears after the symbol # serves as a comment: R does not read these lines (which appear in another color). This allows you to annotate the code to document it, as in this tutorial.

The n_lives object has been created. To display its value in R, all you have to do is... to type its name:

# Display the value of an object by typing its name
n_lives

Calculate with objects

Objects can be used in operations of many kinds, starting with calculations on numerical values. We are thus going to multiply the object n_lives by 2. As we have seen, the multiplication in R is written with the * key

# An arithmetic operation on the n_lives object
2*n_lives

It is possible to create a new object from an existing object and an operation such as a calculation. For example, we are going to create the max_n_lives object to designate the maximum number of lives the hero may have in the adventure :

# An arithmetic operation which is associated with the creation of a new object,
# max_n_lives, to designate the maximum number of hero's lives
max_n_lives <- 2*n_lives
# Display the value of max_n_lives
max_n_lives

Try for yourself!

We suggest you to create the objects n_lives and max_n_lives below, assigning them the values 3 and 6 respectively, then displaying their values. By the way, here's a tip: in the R console, you also can display the solution to get help with the following button:

knitr::include_graphics("images/solution.png")
# Create the object n_lives and max_n_lives


# Display their values


# End of the exercise
# Create the object n_lives and max_n_lives
n_lives<-3
max_n_lives<-2*n_lives
# Display their values
n_lives
max_n_lives
# End of the exercise

Preloaded objects

To help you in your quest, the language of the Runes already has a few “items” already built-in. For example, the value of pi is stored in the object pi.

# Display the value of pi in R
pi

The age of statisians

wzxhzdk:13 **By the way, we still have to answer the question of the mage RegoR… how old is IcaRius?**

In the world of Statis, age follows a somewhat strange logic. At birth, the age of the statisians corresponds to their number of lives multiplied by the number pi squared, from which we subtract the maximum number of lives that they will be able to have during their journey… To facilitate the whole, one will admit to take the rounding of the result...

Ayyayya!

What if we calculated with R? To calculate the square of a number in R, we use the sign ^, such as 3 ^ 2 = 9

# The value of pi squared. To bring a number to the power N, we use ^N
pi^2

Let’s write the formula for the age of IcaRius. Let's take it step by step.

# the number of lives multiplied by the number pi squared...
n_lives * pi^2

# ...minus the maximum number of lives
n_lives * pi^2 - max_n_lives

# That's over!

Not rocket science after all. Like what, this "mage" story in IcaRius is very overrated.

It's your turn!

How about writing the solution to finding the age of IcaRius by yourself? Write your calculation below, run it to find the age... Reminder: the code is executed by pressing the button:

knitr::include_graphics("images/run_code.png")
# The objects n_lives et max_n_lives are already loaded
n_lives <- 3
max_n_lives <- 2*n_lives
# Write the formula to calculate the age of the statisians

# End of the exercise
# The objects n_lives et max_n_lives are already loaded
n_lives <- 3
max_n_lives <- 2*n_lives
# Write the formula to calculate the age of the statisians
(n_lives*pi^2) - max_n_lives
# End of the exercise

Note that we could go further by calculating in R the rounding of the result obtained. But we'll see that later: for now, it's up to you to round off... in your head!

question("So how old is Icarius? All that remains is to check the correct answer... to continue the adventure!",
type="single",
allow_retry = TRUE,
incorrect="Try again",
answer("18 years"),
answer("36 years"),
answer("24 years",correct=TRUE),
answer("The correct answer is not in the proposals"),
correct="Congratulations, you have found the right age, the adventure can continue! Enter the correct value directly into the IcaRius game (via the blue screen) to unlock the rest."
)

End of chapter 1 >> resume the quest of Icarius

(Chapter version : 0.9.1)



InseeFrLab/funcamp-r-grimoire documentation built on Oct. 30, 2023, 3:25 p.m.