We can write any formula and ask for the result
[1] 25
We can also use functions, like square root
[1] 5
Write the function name followed by round parenthesis
The function input goes inside the parenthesis
[1] 3
Use comma to separate multiple inputs
The order is important
For example, logarithm of 3 in base 2 is
[1] 1.584963
Today we talk about four keys: MR
, M+
, M-
, MC
.
beyond English
Each phrase in a program is an order, like
“come here”
Today we will focus on nouns and one verb
The first verb we need today is assign, written as
<-
Nouns are names of objects
To handle objects we give them names
We “store” the objects in variables
If we don’t give a name to an object, it is lost for ever
Like the M button on the calculator
Store to use it later
Variables are created when we assign a value to them
[1] 2
With arrow we assign. There is no output
Without arrow we look at the value
[1] 2
We can only look at a variable that already exists
Error in eval(expr, envir, enclos): object 'b' not found
We try to see a variable that has not been defined
The computer says “that is an error”
In other words, A
is not the same as a
Error in eval(expr, envir, enclos): object 'A' not found
Be careful with small and big caps letters
They are different
Short name for easy things, long name for important things
this_is_a_long_name
thisIsAnotherLongName
this is not a name
Be coherent, be clear
Name starts with a letter, then followed by letters, numbers, dots, underscore
No -
(dash) in the name
No
(space) in the name
Use _
instead
[1] 2
[1] 3
[1] 4
We can use any formula in the right-hand side
The right side is evaluated first, then stored into the variable
We can also say
but it is not recommended
The arrow is more clear
It shows that we take a value and we put it in the variable
We create variables when we assign values. They do not exist before
This is more clear than
which does not make sense in math
It is a command to put new values into a variable
If we do the following steps
What are the values of a
and b
at the end?
NA
Sometimes experiments fail, and we do not get any result
Sometime people do not answer when we ask
Still, we need to register something.
The special value NA
represents missing value
It represents a value that we do not know
<-
stores values in memoryWith arrow we assign. There is no output
Without arrow we look at the value
[1] 2
RMarkdown
Text files, edited with a good text editor
Empty lines between paragraphs
Sections marked with #
and space
Lists start with +
,-
or *
and space
Computer code has three back-ticks before and after
Follow the three back-ticks with {r}
```{r}
2 + 3 * 4
```
Then the code is executed and the result is inserted in the document
[1] 14
Mixing code and text help us to write documents that adapt to the data
Learn more at https://rmarkdown.rstudio.com/lesson-1.html
and that is essential