In the very first time you open RStudio, you will see a window called Console takes up the left side of your screen. The console begins with information about the current R version that RStudio is using, followed by a cursor where you would type your own commands into the Console.
Getting started with R console
Typing a simple 1+1
equation in the Console then hit Enter, you should see a response with the result like this
The response [1] 2
you see above is called a vector.
Vectors is the main data structure in R. A vector is basically a list of objects of the same data type.
Operations in R often, if not always return answers in the form of a vector.
[1] 2
means a vector of one numeric data type with a value of 2.
Hello World in R
Now type in the console these characters with the quotation mark: "Hello World"
.
You should see this response : [1] "Hello World"
.
Like before, this vector has one item, which is a set of characters, or a string.
in R, we can use the letter c
followed by a comma-separated list of objects of the same type to create a vector.
Try typing c("Hello", "World")
into the Console and you should see a response of a two-element vector like this
One may wonder why R console shows [1] while the response contains 2-element vector. The answer is R is saving screen space by only showing item numbers of each row of output. This behavior is better demonstrated with a bigger vector.
Try typing 1:100
into the Console and press Enter, you would see something like this
1:100
in this case means "input all numbers between 1 and 100". The colon (:
) is simply a shorthand for defining a range.
This time, the response shows that each row includes the item position of the data value in the first column of every row.