Starting to use the R Command Line — A Simple Guide…
If you have ever wanted to start using the R command line to assist you with your data scientist career, then this simple guide is for you. I don’t intend to go to great depths on how to use R on the command line, but it is a start — and the goal of this article is to show you the following concepts:
· How to invoke and use the R command line,
· Some very handy — yet simple — commands to use,
· Examples, examples, examples…
Ok, now let’s get into it…
btw — If you have not already installed R, then you can see my previous posts on how to install R (and also the RStudio IDE).
First up, lets fire up the R console:
First and foremost, the R command line/ prompt can be used as simple calculator…
(just quickly too — if you want to quickly clear the console screen, then do the following — In R, press the “Ctrl” + “L” keys simultaneously, and you will see the following cleared R console screen)
Basic Calculator calculations in R console:
Use the R console as a calculator to do the following:
· multiply 10 x 5 x 12
· 20 minus 67,
· 1200 divided by 7
· 90 + 35 + 80
When you type a command at the prompt and hit your <Enter> key, the R console will execute the command, and the results will be displayed on the next line.
You probably have noticed that a
 symbol has appeared alongside the result. R is simply letting you know that this line begins with the first value for your result set.
Basic Objects or Variables
R will let you save calculations and/ or data by storing it inside an R variable or object. So, what is an object? Quite simply it’s just a name that you can use to retrieve of call stored data.
For example, you can save data into an object called
ans. From now on, wherever R encounters the
ans object, it will replace it with the data saved internally, like so:
To create your own R object, choose a variable name (there are rules to follow, though, not covered here) and then use the less than symbol
< followed by a minus sign
- to save data into it.
This combination looks like an arrow as follows:
R will internally create an object, assign it the name you have provided, and store within it whatever follows the
You can now use your newly created
ans object with other new R commands. Since
ans was previously stored the value of 10, by multiplying it by 20 gives a new result -
R can understand the difference with capitalization, or case-sensitive, objects. Therefore, the objects fred and Fred will refer to different object values as follows:
If you have previously created quite a few objects during an R console window session, and you have forgotten which ones you have created, simply use the following command to list them all: ls()
It is Important to note now: R will overwrite any previous object information stored without asking for your permission first. Therefore, it is always a sound idea to not use object names that you have previously used — unless for a specific reason…
R comes with a variety of functions that you can use to do sophisticated tasks like random sampling. For example, you can use the max function to identify and display the maximum number in a provided list of numbers, or calculate two values together using the prod function — as follows:
Using an R function is rather simple. Just type the name of the function and then the data you want the function to operate on in enclosed parentheses.
Most R functions have online documentation with which you can invoke and read. The basic online R command is: help (topic)
So if you want to review the prod function and understand further what it can do, then the command — help (prod) will do the job for you as follows:
This help command will show the follow web-page:
Furthermore, within R, invoking the following menu command:
Shows the following window with some basic but key R commands:
We covered the following in this simple R Console tutorial:
· Simple Calculator Calculations,
· Creating and using R Objects and Variables,
· Basic Mathematical Functions,
· How to retrieve and use Help in R.
As shown in this tutorial, R is a concise language that you can easily and readily use to provide you with simple but effective results. You write commands in R, and then run them at the command line for the application to read and execute. Quite simply, either a result is delivered back to you, or an error message is shown.
The two most critical components of the R language are:
1. Objects and variables which store data, and
2. Functions, which manipulate basic data.
R also uses an assortment of mathematical operators like +, -, *, /, and <- to accomplish basic tasks.
I hope you enjoyed this tutorial, and as an fyi I plan to increase the number, and complexity, of these R Console tutorials over the coming weeks ahead. If you have any feedback for me, please reach out and let me know what’s on your mind..
Stay safe, stay peaceful…
Why not check out my personal professional freelance writer website: www.freelancer-writer.com.au