Starting to use the R Command Line — A Simple Guide…

Image courtesy from Mika Baumeister from Unsplash

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:

The R Console window when you start-up R

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)

Using the “Ctrl” + “L” keys simultaneously will clear the R Console window

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

Basic calculations in R Console

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 [1] 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:

The object ‘ans’ is defined with an initial value of 10, and used in an equation

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 <- arrow.

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 - [1] 200

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:

R treats case-sensitive objects as separate items

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()

The R Console ls() command shows the present and available objects to use

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…

Functions

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:

The max and prod functions in use in R

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.

Getting Help

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:

The help command for the prod function

This help command will show the follow web-page:

The web-page help page for the prod function

Furthermore, within R, invoking the following menu command:

How to invoke help in R

Shows the following window with some basic but key R commands:

Basic but important R Console commands

Summary

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.

Image courtesy from Roman Synkevych from Unsplash

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…

Image courtesy from Dieny Portinanni from Unsplash

Why not check out my personal professional freelance writer website: www.freelancer-writer.com.au

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Paul Chambiras

I am a freelance writer on all things Business, DIY, Sport, Technology, IT and Management.