Start R Studio
Use the console (indicated by the arrow) to enter commands
Write an arithmetic operation such as 2 + 5 and press Enter
> 2 + 5
> [1] 7
>
[1] means just that this line begins with the first value in your results.
> is the prompt waiting for your next command.
You can use all standard mathematical operations such as +, -, /, *, ^.
You can also use brackets () to order the evaluation of the operations.
> (3 * 7) / (2^4)
[1] 1.3125
>
Sometimes your output is longer then a single number.
> 1:60
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
[24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
[47] 47 48 49 50 51 52 53 54 55 56 57 58 59 60
>
The colon operator : helps us to generate a sequence between two numbers.
The first line begins with [1] to indicating this it starts of the 1st value,
the second line begins with [24] indicating it starts of with the 24th value,
the third line begins with [47] indicating it starts of with the 47th value.
When you start writing a command and press Enter before finishing, R will display + indicating it waits for you to continue. You can either finish the command or press Esc to start over.
> 5 -
+
+ 2
[1] 3
If you ask R to do something unexpected, it will give you an error.
There is no reasons to panic, after an error you still have the prompt > and you can try again something that will work.
> 3 % 5
Error: unexpected input in "3 % 5"
>
Sometimes executing a command may take long (often when you made an error).
To cancel execution press Ctrl + c (though is may as well take long to cancel the command).
From now on, in this tutorial I will drop the prompt > in front of the R commands (in gray boxes),
and the results well be preceding by ## (in white boxes).
This should be clearer to read and make it easier for you to copy-paste pieces of code.
3 + 5:10
## [1] 8 9 10 11 12 13
In R, # indicates a comment. Everything to the right of it is ignored by R. It may be useful to add your explanations to the code.
Imagine you have come up with some complicated calculation.
((5^7 / 12 ) * (3^4)) - (24 * 2)
## [1] 527295.8
R calculates and gives you the result.
But every time you would like to to use result you either need to write the formula again, or copy paste the result in.
There is a better way.
You can store the result into an R object giving it a name (string).
Whenever you use the object thereafter, R will recall its value.
First assign the calculation to an object. We will name the object for example a.
a <- ((5^7 / 12 ) * (3^4)) - (24 * 2)
Instead of printing the results, R will store it into the object a (seemingly giving you know result). Anytime later, you can ask R what the value in a is.
a
## [1] 527295.8
You can then do some more unrelated calculation
3+5 * 12
## [1] 63
but R still remembers the value in a
a
## [1] 527295.8
and you can also use it in further calculations
a + 2
## [1] 527297.8
The a object above contained just a single number.
But R objects can be more complicated, for example an object can contain a sequence of numbers.
seq_object <- 1:50
seq_object
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50
The objects a and seq_object are both vector objects. The difference is, that a is a vector with only one element, while seq_object has 50 elements.
To create a vector with your chosen elements use the c() function.
my_vec <- c(3,8,2,4)
my_vec
## [1] 3 8 2 4
The vectors above all contained numbers ~ objects of numeric class.
class(my_vec)
## [1] "numeric"
Vectors may contain objects of other classes such as character or logical (FALSE/TRUE).
text_vector <- c("First", "Second", "Third")
text_vector
## [1] "First" "Second" "Third"
class(text_vector)
## [1] "character"
bool_vector <- c(T, F, FALSE, TRUE, F, FALSE)
bool_vector
## [1] TRUE FALSE FALSE TRUE FALSE FALSE
class(bool_vector)
## [1] "logical"
However, classes shall not be mixed within a single vector. When you mix, the vector will be of the most generic class (typically character).
mixed_vector = c(FALSE, "Correct", 1)
mixed_vector
## [1] "FALSE" "Correct" "1"
class(mixed_vector)
## [1] "character"
You can change the class of an object (coerce) using as.* if the new class make sense for the object.
num_object <- 1.5
class(num_object)
## [1] "numeric"
as.character(num_object)
## [1] "1.5"
char_object = c("1.23", "2.45", "3.14")
class(char_object)
## [1] "character"
as.numeric(char_object)
## [1] 1.23 2.45 3.14
char_object = c("1.23", "2.45", "hello")
class(char_object)
## [1] "character"
as.numeric(char_object)
## Warning: NAs introduced by coercion
## [1] 1.23 2.45 NA
The last element cannot be converted to number so R produces NA, a missing value (Not Available).
You can give your objects any name you like as long as it is starts with a letter (a-z,A-Z) and contains only letters, numbers and underscores _.
For example my_object, resultOfCalcul2 or WOW_RESULT are all fine.
These are not fine: 2nd_result, price$ or email@address.
Also R is case-sensitive so lower-case and upper-case letter are not the same. My_result is not equal to my_result.
You can see the object names that you defined so far by using the function ls()
ls()
## [1] "a" "bool_vector" "char_object" "mixed_vector"
## [5] "my_vec" "num_object" "seq_object" "text_vector"
Alternatively, you can see the objects in the Environment pane of the R studio
R will overwrite any information in an object without asking permission so you should be careful about the names you use.
a <- 1111
a
## [1] 1111
R comes with many predefined functions to do more complicated things then simple mathematical operations. You call a function using its name followed by brackets (arg) inside which you put the argument(s) of the function. For example:
sqrt(36)
## [1] 6
sum(5:10)
## [1] 45
mean(seq_object)
## [1] 25.5
var(5:25)
## [1] 38.5
You can also combine multiple functions together
round(mean(seq_object))
## [1] 26
This first calculates the result of mean(seq_object) (which we calculated before as 25.5) and then rounds to get the final 26.
All the functions above operated over a single input, single argument. Functions may take multiple arguments. For example seq() which generates a sequence of numbers accepts the following 3 arguments: from where to begin, where to go, and by steps of what size.
seq(from=3, to=15, by=2)
## [1] 3 5 7 9 11 13 15
You do not have to give the argument names when calling a function and giving the arguments in the correct order
seq(3, 15, 2)
## [1] 3 5 7 9 11 13 15
There are many Many MANY functions in R and nobody knows them all.
The best way to figure out how to do something in R is to Google, you will find plenty of help online.
To get help for a function you can use either ? or help() to R Documentation page in R studio (Help pane in the right bottom corner).
?seq
help(seq)
You can also search the R documentation using ??
??regression
One of the many useful sources available on the internet is the R cheat sheet.
Writing code in console is quick and easy but soon it becomes difficult to trace back what you did. Instead of typing and executing expressions one-by-one, you may want to write multiple lines of expressions (code) and evaluate them all at once. This you can do by using R scripts. R script is just a text file (with extension *.R) containing all the R commands you want to execute line-by-line.
To open a new empty script go to File menu and select New File - R Script (or press `Ctrl+Shift+N’). This will open the editor pane in your R Studio with an empty Untitled script.
Instead of writing your commands directly into the console, you can write them into the script in the editor pane. However, writing a command in the script and pressing Enter will simply move your cursor to the next line as in any other text editor.
To execute (evaluate) commands in the script you place the cursor onto the respective line in the script you want to execute and press Ctrl+Enter.
This will copy for you the piece of code into the console, evaluate the expression, and move the cursor in the script onto the next command. Like this, you can run all commands in your script by repeatedly pressing Ctrl+Enter.
You can execute multiple lines by By pressing Ctrl+Shift+Enter you will run all commands in the script in one go.
Why use scripts? Because you can save the script file anytime you like File - Save or by pressing Ctrl + S (give it some nice name). All your commands (and your useful comments preceded by #) are in it and therefore you will not lose your precious code.