Excel Unplugged

R Beginner Tutorial – Basic Syntax

This is part 2 of our R Beginner Tutorial. For part 1, head over to R Beginner tutorial.

Let’s look at basic syntax in R. We’ll learn how to use basic operators, functions, comments, R files and packages.

Arithmetic operators

R knows the following arithmetic operators.

OperatorDescription
+Addition
Subtraction
*Multiplication
/Division
^ or **Exponent
%%Modulus (Remainder from division)
%/%Integer Division
Arithmetic operators in R

For example:

> x=10
> y=2

> x+y
[1] 12

> x-y
[1] 8

> x*2
[1] 20

> x/y
[1] 5

> x^y
[1] 100

> x**y
[1] 100

> x%%y
[1] 0

Relational operators

Similarly, we can use the following relational operators.

OperatorDescription
Less than
Greater than
<=Less than or equal to
>=Greater than or equal to
==Equal to
!=Not equal to
Relational operators in R

For example:

> x=10
> y=2

> x<y
[1] FALSE

> x>y
[1] TRUE

> x<=y
[1] FALSE

> x>=y
[1] TRUE

> x==y
[1] FALSE

> x!=y
[1] TRUE

Comments

Comments start with # symbol. For example:

#This is a comment

Print

Printing is done by simply using the print() function.

> x = "I love R!"
> print(x)
[1] "I love R!"

R beginner tutorial

Math functions

There of course is an abundance of available math functions. Few examples are listed in the table below.

FunctionWhat It Does
abs(x)Takes the absolute value of x
log(x,base=y)Takes the logarithm of x with base y; if base
is not specified, returns the natural logarithm
exp(x)Returns the exponential of x
sqrt(x)Returns the square root of x
Math functions in R

Paths and files

We use getwd() function to get a location of a working directory.

> getwd()

\\\\192.168.30.6/Home/gasperk/My Documents"

We use setwd() function to set the location of working directory.

> setwd("C:/Users/gasperk")

We use list.files() function to lčist all the files in a directory.

> list.files()
  [1] "AppData"                                                                                     
  [2] "Application Data"                                                                            
  [3] "ChannelData.xlsx"                                                                            
  [4] "Contacts"                                                                                    
  [5] "Cookies"                                                                                     
  [6] "Desktop"  
…

Help

We can find help for any function by typing help(function_name). For example, help for log function:

> help(log)
R Beginner Tutorial - Basic Syntax, help on functions
Help on logarithm function in R

In conclusion, this sums up all the R syntax basics. Tune in next week to discuss data structures!

In a hurry?

If you’re already running late, we recommend you head over to our two R crash courses:

For even more amazing tips check out our awesome ExcelOlympics YouTube channel!

Share this:

Share on email
Email
Share on linkedin
LinkedIn
Share on facebook
Facebook
Share on twitter
Twitter
Share on pinterest
Pinterest

Related posts