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

For example:

``````> x=10
> y=2

> x+y
 12

> x-y
 8

> x*2
 20

> x/y
 5

> x^y
 100

> x**y
 100

> x%%y
 0
``````

## Relational operators

Similarly, we can use the following relational operators.

For example:

``````> x=10
> y=2

> x<y
 FALSE

> x>y
 TRUE

> x<=y
 FALSE

> x>=y
 TRUE

> x==y
 FALSE

> x!=y
 TRUE
``````

``#This is a comment``

## Print

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

``````> x = "I love R!"
> print(x)
 "I love R!"
``````

## Math functions

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

## 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()
 "AppData"
 "Application Data"
 "ChannelData.xlsx"
 "Contacts"
 "Desktop"
…
``````

## Help

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

``> help(log)``

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!