- ExcelUnplugged
- November 5, 2021
- 0 Comments

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.

R knows the following arithmetic operators.

Operator | Description |

+ | Addition |

– | Subtraction |

* | Multiplication |

/ | Division |

^ or ** | Exponent |

%% | Modulus (Remainder from division) |

%/% | Integer Division |

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

Similarly, we can use the following relational operators.

Operator | Description |

< | Less than |

> | Greater than |

<= | Less than or equal to |

>= | Greater than or equal to |

== | Equal to |

!= | Not equal to |

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 start with **#** symbol. For example:

`#This is a comment`

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

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

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

Function | What 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 |

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

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!

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

- R Language overview, fast and easy beginner tutorial on R
- ggplot2 Basics, fast and easy beginner tutorial on plotting data in R

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

R Beginner Tutorial – Arrays

- November 24, 2021

In previous posts, we learned vectors, lists and matrices. ...

R Beginner Tutorial – Matrices

- November 20, 2021

In previous posts, we learned vectors and lists. Vectors are ...

R Beginner Tutorial – Lists

- November 19, 2021

In previous post, we learned vectors are the most basic data ...

R Beginner Tutorial – Vectors

- November 12, 2021

Vectors are the most basic R data structure. We already know ...