Another little step towards the machine learning: A first lesson about R language
Hi Guys,
Welcome Back!
Today another little step towards the machine learning with a first lesseon about the R language.
Introduction
Yes, you remember very well!We already talked about SQL Server and the R language some posts ago.
In a first post we talked about how to install the machine learning services in SQL Server
While in a second post we talked briefly about using R language to read data from SQL Server
Now if you want to do another step towards the machine learning you really need to learn a bit of R language.
So today I will show some very basic concepts about this really powerful and widely used in data science, the R language.
We will se how to do and assignment, how to use array, vector and matrix.
Are you Ready?
Let's go!
A Basic lesson about R!
R is a very powerful language and it is also free and expandible through varius library.
I suppose you have already installed R and so you a ready to execute the R Console.
You can start typing some after the ">" prompt
You can, for example, type the text below and then press enter:> 8+2
You will obtain:
[1] 4
Type now:
> log(2)
and you will receive:
[1] 0.6931472
Ok R is a fantastic calculator!!! But let’s continue...
Variables
Some note:
- R declare automatically variables
- The assignment is done with the <- command
- R is case sensitive
You can declare a variable a and assign the value 4 typing:
> a <- 4
Now if you type:
> a
You will obtain the result of the variable "a"
[1] 4
Now if you type:
> a
You will obtain the result of the variable "a"
[1] 4
Vectors (Arrays)
R can manage vectors (array) in an easy way
Type for example the command below:
List <- c(1, 2, 2.5, 9, 9, 9, 5, 2)
Through the “c” concatenate command you declare a Vector
In order to show the content og the vector just type:
> List
[1] 1.0 2.0 2.5 9.0 9.0 9.0 5.0 2.0
Nice!
You can also get a subset of your vector with this syntax:
> List [1:3]
[1] 1.0 2.0 2.5
Or you can easily sum the values of all the elements of your vector:
sum(List) [1] 440.5
Another way to declare a vector is this:
> W <- vector(length = 2)
> W[1] <- 10
> W[2] <- 12
[1] 1.0 2.0 2.5 9.0 9.0 9.0 5.0 2.0
Nice!
You can also get a subset of your vector with this syntax:
> List [1:3]
[1] 1.0 2.0 2.5
Or you can easily sum the values of all the elements of your vector:
sum(List) [1] 440.5
Another way to declare a vector is this:
> W <- vector(length = 2)
> W[1] <- 10
> W[2] <- 12
And now i will show you how to manage Matrix!
Matrix
You can declare a matrix through this Syntax:
> Mat <- matrix(nrow = 8, ncol = 4)
To display the Matrix content:
> Mat
[,1][ ,2][ ,3][ ,4]
[1,] NA NA NA NA
[2,] NA NA NA NA
[3,] NA NA NA NA
[4,] NA NA NA NA
[5,] NA NA NA NA
[6,] NA NA NA NA
[7,] NA NA NA NA
[8,] NA NA NA NA
Matrix
You can declare a matrix through this Syntax:
> Mat <- matrix(nrow = 8, ncol = 4)
To display the Matrix content:
> Mat
[,1][ ,2][ ,3][ ,4]
[1,] NA NA NA NA
[2,] NA NA NA NA
[3,] NA NA NA NA
[4,] NA NA NA NA
[5,] NA NA NA NA
[6,] NA NA NA NA
[7,] NA NA NA NA
[8,] NA NA NA NA
And set a value with:
Mat [1, 1] <- 2.0
Mat [1, 1] <- 2.0
That’s all for today!
I hope you liked this post and found it useful.
Luca
SQL: READY TO RUN FASTER?
Previus post: SQL Server, find text in a Trigger, Stored Procedures, View and Function. Two ways and what ways is better
ReplyDeletethank you for the valuable information giving on data science it is very helpful.
Data Science Training in Hyderabad
Data Science course in Hyderabad
Data Science coaching in Hyderabad
Data Science Training institute in Hyderabad
Data Science institute in Hyderabad