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.
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.
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
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
my_var <- 3 + 2 TRUE -> my_var_2 # my_var_2 TRUE # my_var 5
This was the fun part for me. Vectors are the simplest data type in R. It contains elements of the same type.
x <- c(1,2,3,4) #  1 2 3 4 y <- c("test","test2") #  "test" "test2" z <- c(TRUE,2,"test") #  "TRUE" "2" "test"
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
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
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 # 32 x # 1 x[2:3] # 21 1 x[-1] # 3
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
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
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.
📰 My 3 recent posts: