To return to the previous page click here or use the back button on your browser.
Excel is notorious for converting values from one type to another inappropriately.
“With power comes great responsibility” - Uncle Ben
With this extra capability, we need to understand a little about:
We will also briefly touch on:
RStudio
thenFile
> New File
> R Script
RTutorial.R
Console
by selecting a line/section, then either
Ctrl + Enter
Run
button at the top right of the Script Window
Let’s create an R object called x
Script Window
, then send it to the Console
NB: R is case-sensitive so please be careful
x <- 5
We can see the contents of the object x
by just entering it’s name in the Console
(or in the Script Window
& sending it to the Console
)
x
## [1] 5
In the R Environment, we can create objects of multiple types. We first give them a name (e.g. x
) and then assign a value to it using the <-
symbol. This is like an arrow putting the value into the object.
R objects are only visible when we enter the object name in the Console
.
The R Environment is like a desk (or Workspace) where we can leave things out of view until we need them. In RStudio, the Environment
tab in the top right shows what’s currently in our Environment. This is the list of objects we can perform operations (or analyses) on.
Nothing is saved to disk, until we save the complete Environment as an .RData
object. We can do this by clicking the disk symbol in the Environment Tab, or by entering the code:
save.image()
By default, R will save the workspace as a file named .RData
and this will contain all of the R objects you currently have in your environment.
What is the difference between these two?
The first represents the value 7, whilst the second is the character that we use to represent this in text. To your computer these mean different things!
Excel will automatically try to convert “7” to the value 7, but automatic conversion can be troublesome. In R we maintain control over this delineation.
The basic (or atomic) data types in R are:
logical
(TRUE
or FALSE
)integer
(e.g. the value 5
that we gave to x)double
(also known as numeric
)character
When would you need an integer rather than a double?
matrix
Some examples of logical tests/values
x > 0
## [1] TRUE
x == 5
## [1] TRUE
x < 0
## [1] FALSE