## Intro

Recently I've enrolled in a university course "Mathematical modeling and Data analysis". In the future, I plan to combine my front end development skills with data science working on product analysis and business growth.

I started learning R lang 3 weeks ago at University and using DataCamp. In this article, I'll show you the basics I learned so far.

## More about R

R is a free and open-source language. It's an awesome choice when it comes to data manipulation, mathematic, and statistic. It's used by many big players and equally with Python is a great weapon for every Data Scientist.

To write R code you can either download R studio or start with an online tool like repl.it

## Basic calculations

Almost everything is as you may be expected. What's interesting is how we get a remainder (`%%`

) and a result of division (`%/%`

)

```
10 + 5 # 15
10 - 5 # 5
10 * 2 # 20
10/3 # 3.33333
5.5 %% 3 # 2.5
7 %/% 4 # 1
```

## Variables

In R you can use variables like in every programming language but you don't assign values using `=`

. The preferable way to do that is by using `->`

(rightward operator) or `<-`

(leftward operator).

```
my_var <- 3 + 2
TRUE -> my_var_2
# my_var_2 TRUE
# my_var 5
```

## Vectors

This was the fun part for me. Vectors are the simplest data type in R. It contains elements of the same type.

### Declaration

```
x <- c(1,2,3,4) # [1] 1 2 3 4
y <- c("test","test2") # [1] "test" "test2"
z <- c(TRUE,2,"test") # [1] "TRUE" "2" "test"
```

### Naming

To avoid confusion while using your data later on you can name data inside your vector.

```
x <- c(1.90,89,25)
names(x) <- c('Height [m]','Weight [kg]', 'Age')
# Height [m] Weight [kg] Age
# 1.90 89 25
```

### Calculations

You don't need to use loops to make calculations on vectors in R it's a built-in behavior.

```
c(1,2,3) + c(4,5,6) # 5,7,9
c(1,2,3) - c(4,5,6) # -3,-3,-3
c(1,2,3) * c(4,5,6) # 4,10,18
c(1,2,3) / c(4,5,6) # 4,10,18
c(1,2,3) %% c(4,5,6) # 1,2,3
c(4,5,6) %/% c(1,2,3) # 4,2,2
```

### Selecting elements

The way how we select elements is very similar to other languages, but **R indexes elements from 1**

```
x <- c(32,21,1,3)
x[1] # 32
x[3] # 1
x[2:3] # 21 1
x[-1] # 3
```

### Selecting elements by comparison

Sometimes you want to get values from vector meeting specific criteria. In JavaScript, you would do this using .filter() but in R we can achieve this in a slightly different way.

```
x <- c(2,3,9,10,11)
x %% 2 == 0 # TRUE FALSE FALSE TRUE FALSE
```

As you can see we're trying to get all even numbers from the vector `x`

.

But the expression above returns a vector with logical values representing a condition state for every element.

```
x <- c(2,3,9,10,11)
x[x %% 2 == 0] # 2 10
```

When we want to actually get those elements we need to put the condition inside the brackets.

As you can see it only select those elements where the condition value was `TRUE`

.

## Sum up

That's it for today. I hope I encouraged you to give R a try. If you know any good resources for learning R/Data Science/Data Analysis please let me know in the comments I'd love to find more resources for learning those.

🐦 Follow me on Twitter for a web dev & side projects content

👉 Check the SaaS I'm building right now EmbedTables - Beautifully embed Google Sheets or Airtable data into website in no time.

📰 My 3 recent posts:

## Top comments (0)