<- 1
x x
[1] 1
= 1
x x
[1] 1
At the end of this lesson, you should:
It is rather cumbersome to continually retype or paste information. We can avoid this by assigning information to an R object.
Traditionally in R, the left arrow is used for object assignment, <-
(the less-than symbol and a dash), but the standard equals sign, =
also works.
These are equivalent:
<- 1
x x
[1] 1
= 1
x x
[1] 1
We can assign multiple numbers to an object:
<- 1:10
x_vector <- c(2, 4, 6, 8, 10) y_vector
The left arrow assignment <-
takes everything on the right side of the arrow and assigns it the object name on the left.
It is your choice (mostly) about what to name R objects. There are a few rules and conventions to follow:
test
is different from Test
and TEST
. Be mindful of this! It trips many folks up..
and _
).variable
), you won’t see it listed in the global environment (which can be frustrating). This is not recommended for newer R users.Here is some discussion on object naming in R.
reserved word | meaning |
---|---|
TRUE FALSE |
logical |
NA |
missing value |
NaN |
not a number/undefined |
NULL |
no value/undefined |
Inf -Inf |
infinity |
for in |
for loops |
if else while break next repeat |
control flow |
NA_integer_ NA_real_ NA_complex_ NA_character_ |
missing data by data type |
It’s easy to forgot these. Run ?reserved
in an R console or check here to remind yourself if need be.
Some examples of reserved words in the wild:
log(0)
[1] -Inf
0/0
[1] NaN
2/0
[1] Inf
These are the same:
4/3
[1] 1.333333
4/ 3
[1] 1.333333
4 / 3
[1] 1.333333
These are also the same:
log(10)
[1] 2.302585
log( 10 )
[1] 2.302585
log ( 10)
[1] 2.302585
R expects certain things to be paired or completed before it will send it to the interpreter
As mentioned, earlier a hard return is sufficient to send a command to the R interpreter.
Exceptions: binary operators (= those expecting 2 numbers): +
, -
, *
, /
, ^
, ==
, etc. R is waiting for these to be ‘completed’.
Exceptions: unclosed parentheses ()
, brackets []
{}
, or quotes ' '
" "
. R will wait for these to be completed. A single quote must always be complemented by a second single quote, and a double quote likewise must always have a second quote to complete it. Left parentheses, curly braces, or brackets much also be accompanying by their right-sided complement.
Examples
1 + 2
{ }
( )
[ ]
" "
' '
` `
1 +
'
( } ]
"
' "
Example:
"r `format(Sys.Date(), '%b %d, %Y')`"
[1] "r `format(Sys.Date(), '%b %d, %Y')`"
If this particular piece of code makes no sense to you, do not worry. The point of presenting this code at this stage in your journey of learning R is to demonstrate how single quotes, double quotes and the backtick ` can be used together in single statement.
Matching parentheses, quotes, and other paired structures is important in R. The R interpreter may stop if it is waiting for a statement to be ‘closed’. As a result, RStudio will often automatically append a pair while you type. Try typing a single quote, double quote, square bracket, curly bracket, parentheses or backtick and notice how this happens.
Rstudio furthers this practice when you highlight text. In RStudio, highlight some text and then type the key for double quotes. What happened? Try the same with parentheses and the other keys/symbols mentioned. Once you get used to this, it will save you some time!