0% found this document useful (0 votes)
10 views11 pages

R Basics: A Beginner's Guide

The document is a comprehensive guide to getting started with R and RStudio, covering essential topics such as data types, vectors, functions, loops, and data visualization. It highlights the importance of R for statistical computing and provides an overview of the RStudio interface. The author aims to inspire readers to continue exploring R's capabilities beyond the basics presented in the workshop.

Uploaded by

shivig1979
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

R Basics: A Beginner's Guide

The document is a comprehensive guide to getting started with R and RStudio, covering essential topics such as data types, vectors, functions, loops, and data visualization. It highlights the importance of R for statistical computing and provides an overview of the RStudio interface. The author aims to inspire readers to continue exploring R's capabilities beyond the basics presented in the workshop.

Uploaded by

shivig1979
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/8/23, 10:33 PM R Basics — Everything You Need to Know to Get Started with R | by Jason Chong | Towards Data Science

Open in app Sign up Sign In

Search Medium

Published in Towards Data Science

You have 2 free member-only stories left this month. Sign up for Medium and get an extra one

Jason Chong Follow

Sep 26, 2021 · 6 min read · · Listen

Save

R Basics — Everything You Need to Know to Get


Started with R
Vectors, functions, loops, probability, data visualisations, and more!

60

[Link] 1/11
2/8/23, 10:33 PM R Basics — Everything You Need to Know to Get Started with R | by Jason Chong | Towards Data Science

Photo by Diego PH on Unsplash

As part of my company’s university outreach initiative, I recently gave a presentation at


the University of New South Wales (UNSW) Women in Technology (WIT) society on the
[Link] 2/11
2/8/23, 10:33 PM R Basics — Everything You Need to Know to Get Started with R | by Jason Chong | Towards Data Science

fundamentals of R and RStudio.

In efforts to spread the content to a wider audience, I have decided to put together this
blog post to recap what was discussed in that presentation.

In summary, the topics of the presentation are as follows:

Introduction to R and RStudio

RStudio interface and basics

Basic arithmetic and variable assignment

Comparison and logical operators

Data types

Vectors

Functions

Loops and conditionals

Probability and statistics

Data frames

Data visualisation

To watch the full presentation or to access the workshop material, kindly check out the
WIT website here.

Introduction to R and RStudio


R is a popular programming language for statistical computing and graphics. It is
widely used among statisticians and data miners for developing statistical software and
data analysis.

[Link] 3/11
2/8/23, 10:33 PM R Basics — Everything You Need to Know to Get Started with R | by Jason Chong | Towards Data Science

RStudio, on the other hand, is an Integrated Environment Environment (IDE) for R that
is available in two formats: RStudio Desktop, which is a regular desktop application,
and RStudio Server, which runs on a remote server and allows access to RStudio via a
web browser.

For the purpose of this workshop, we will be using RStudio Deskstop which you can
find here and install on your local computer.

So, why should you learn R?

R is open-source, which means that it is constantly being updated and improved by


other collaborative developers around the world

It has many external packages that are suited for different purposes e.g. data
manipulation, text cleaning, data visualisation, and more

It is relatively easy and straightforward to pick up once you are familiar with the
basic syntax

Almost everyone who works with data understands and knows how to use R, so you
should know it too!

RStudio interface and basics

[Link] 4/11
2/8/23, 10:33 PM R Basics — Everything You Need to Know to Get Started with R | by Jason Chong | Towards Data Science

RStudio interface

RStudio is split into 4 quadrants:

Script (top left): where commands are written, executed, and saved

Environment (top right): lists the data, variables, and functions that are currently
in the workspace

Console (bottom left): for quickly testing code and where commands and outputs
are displayed, except plots

Plot (bottom right): where graphics are displayed

As for basic commands that you need to know when using RStudio:

Clear console: Ctrl + L

Quit RStudio: Ctrl + Q or quit()

Run code from the script: Ctrl + Enter

Remove saved variable: remove()

Clear everything in the workspace: remove(list = ls())

Access previous command: Arrow up

R awaits the next command: >

R is expecting more inputs: +

Comment / uncomment code in script: Ctrl + Shift + C

Getting help: help() or ?

Basic arithmetic and variable assignment


One of the most basic use cases of R is basic arithmetic. Moreover, you can also assign
values to variables in order to make your calculations more flexible and robust.

Add: +

[Link] 5/11
2/8/23, 10:33 PM R Basics — Everything You Need to Know to Get Started with R | by Jason Chong | Towards Data Science

Subtract: -

Multiply: *

Divide: /

Power: ^ or **

Integer divide: %/%

Modulo (remainder after division): %%

Variable assignment: = or <-

Comparison and logical operators


Comparison operators compare a pair of values and return either a true or a false.

Equal to: == (note the difference between == , which is used for comparison and
=, which is used for assignment)

Not equal to: !=

Greater than: >

Less than: <

Greater than or equal to: >=

Less than or equal to: <=

Logical operators, on the other hand, are used to combine multiple true and false
statements.

And: &

Or: |

Data types
There are 3 main data types in R:

[Link] 6/11
2/8/23, 10:33 PM R Basics — Everything You Need to Know to Get Started with R | by Jason Chong | Towards Data Science

Numeric: numbers e.g. 0, 3.5

Character: can contain letters, numbers, and special characters e.g. “hello, world”

Logical: boolean values i.e. true or false

Vectors
Vector is the most fundamental data structure that is used to store data in R. Vector is a
one-dimensional, ordered collection of data of the same data type.

To manually create a vector in R, we use c( ) . Alternatively, there are also built-in


functions in R specifically for the purpose of creating numeric vectors such rep( )

and seq( ) .

We can also access, add, remove, and alter the elements in any given vector.

Functions
Programming, much like any problem-solving scenario in general, is about breaking
down a big problem into smaller constituent components. This helps to not only better
structure and organise our work but more importantly, it allows for easier code review
and debugging when needed.

Functions are a piece of code that performs a certain task that can be readily reused
again. A function involves a simple 3-step process: input, process and output.

Inputs are sometimes called parameters or arguments which is what we pass into the
function itself. Process is what the function will perform on the inputs that it is being
given, which can be any form of data transformation or numerical computation. Last
but not least, the function will then return the desired output.

In addition to using the built-in functions in R, we can also construct our own function
using function( ) .

Some built-in functions in R include:

abs( )

[Link] 7/11
2/8/23, 10:33 PM R Basics — Everything You Need to Know to Get Started with R | by Jason Chong | Towards Data Science

sqrt( )

round( )

log( )

exp( )

sin( )

Loops and conditionals


Loops repeatedly run a piece of code for a given number of times or until a condition
has been met. To define a condition in loops, we need if-else statements.

There are two types of loops in R which is consistent across many other programming
languages:

For loop: run a piece of code many times

While loop: keep running a piece of code until some condition fails

Probability and statistics


R was mainly built for statistical analysis and handling large volumes of data.

It has several built-in functions that enable summary statistics, probability


distributions as well as hypothesis testing to be carried out easily:

Summary statistics are used to summarise a set of observations e.g. summary( )

Probability distributions assign probabilities to different outcomes e.g. dbinom( ) ,

pnorm( ) , qchisq( ) , and rexp( )

Hypothesis testing offers a way to test the result of an experiment e.g. [Link]( ) ,

[Link]( ) , and [Link]( )

Data frames
Similar to a vector, data frame is data structure that is used for storing data in R. It is a
list of vectors which are of equal lengths but can be of different data types.
[Link] 8/11
2/8/23, 10:33 PM R Basics — Everything You Need to Know to Get Started with R | by Jason Chong | Towards Data Science

The vectors are presented as columns, each with a name and represent variables. The
rows represent observations and can be named or unnamed.

While they can be constructed from scratch, data frames are typically produced from
importing data sets e.g. csv or Excel format files.

Data visualisation
A picture is worth a thousand words. A good data scientist is able to communicate
findings and persuade stakeholders through effective data visualisations.

Though the workshop covers a more advanced visualisation tool, that is the ggplot

package in tidyverse , there are still a variety of built-in visualisation functionalities in


R that do not require any external packages.

For that reason, I would recommend that you play around with the following built-in
functions first before exploring other external packages:

Scatter plot or line plot: plot( )

Add graph on top of existing plot: points( )

Draw straight lines on existing plot: abline( )

Box plot: boxplot( )

Histogram: hist( )

Column graph: barplot( )

Pie chart: pie( )

With that, we conclude the basics of using R and RStudio.

Please note that the workshop merely presents the fundamentals of R and is barely
scratching the surface of what R is actually capable of, especially when combined with
other robust external packages.
[Link] 9/11
2/8/23, 10:33 PM R Basics — Everything You Need to Know to Get Started with R | by Jason Chong | Towards Data Science

Nevertheless, I hope the workshop will be able to help you get started with using R but
more importantly, inspire you to continue to discover what you can do using the
programming language.

Thank you for reading and happy learning!

WIT x Quantium Presents: R Coding Basics Workshop

Data Science Machine Learning R Programming Technology

Sign up for The Variable


By Towards Data Science

Every Thursday, the Variable delivers the very best of Towards Data Science: from hands-on tutorials and cutting-edge
research to original features you don't want to miss. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review
our Privacy Policy for more information about our privacy practices.

[Link] 10/11
2/8/23, 10:33 PM R Basics — Everything You Need to Know to Get Started with R | by Jason Chong | Towards Data Science

Get this newsletter

About Help Terms Privacy

Get the Medium app

[Link] 11/11

You might also like