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

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
``````

``#This is a comment``

## Print

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

``````> x = "I love R!"
> print(x)
[1] "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()
[1] "AppData"
[2] "Application Data"
[3] "ChannelData.xlsx"
[4] "Contacts"
[6] "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!

## Related posts

### R Beginner Tutorial – Arrays

R Academy Menu ggplot2 basics: learn ggplot2 in 15 minutes!R Beginner TutorialR Beginner Tutorial – Basic SyntaxR Beginner Tutorial – R ObjectsR Beginner Tutorial –

### R Beginner Tutorial – Matrices

R Academy Menu ggplot2 basics: learn ggplot2 in 15 minutes!R Beginner TutorialR Beginner Tutorial – Basic SyntaxR Beginner Tutorial – R ObjectsR Beginner Tutorial –

### R Beginner Tutorial – Lists

R Academy Menu ggplot2 basics: learn ggplot2 in 15 minutes!R Beginner TutorialR Beginner Tutorial – Basic SyntaxR Beginner Tutorial – R ObjectsR Beginner Tutorial –

### R Beginner Tutorial – Vectors

R Academy Menu ggplot2 basics: learn ggplot2 in 15 minutes! R Beginner Tutorial R Beginner Tutorial – Basic Syntax R Beginner Tutorial – R Objects