Vector: Definition-Operation
Vector is the base element used by R, each vector after beeing defined can be use as single variable or like a part of an operation that we can use with other data structures and later we'll see that it can also be used in combined operations for matrix and data frame analize.
![]() |
| Fig 1 |
Did it? Ok Since you’ve lost a lot’s of matches let me remind you that a chessboard is a big square created with small square inside, where in every little square you just put a checker and move it to win the match, but I tell you in my chessboard we use lottery pieces with numbers over them instead of spawn and I also use a very flexible chessboard where we can increase or decrease the relative size when we want, this will be done at the end of each row and column.
Watch at Fig.1, we have drawn our chessboard and used numbers to identify every single position, in this case to find the 7-piece you have to move 3 horizontal squares from left to right and 2 from top to down.
I don’t want to concentrate on the rules of chess game or something, in my tutorial i just want to see where they are positioned and how to put them in the chess position in R.
We start playing with a single row or column, so let’s say we start with a simple row and we disconnect it from the chessboard where we can put 8 spawns on it, one for every square.
Since it’s just a single row (or it could be a single column as well) we don’t need to say for example move 3 squares down and 2 squares from left to right because in case of an 8-row pieces we can just say “move to right a n° of times from the origin” or “move down n° of times from the origin”(column case).
| Fig 2 |
created your first vector and u can see it in fig 2!
In this simple vector if you want for example to identify the number into the square 7 you just move from left to right by 7 times.
And what about a column vector? The approach is the same, what can change is just the visualization of this vector but everything works at the same manner.
We are talking about R and even if it’s fun to spent a lot of time playing with a chessboard it’s time to see how a vector-row from a chessboard can be defined with R.
Def:
A single Vector in R is defined in the following way:
"Vector Name"<-c("Element in Position 1", "Element in Position 2"..............,"Element in Position n")
R is case-sensitive and "Vector Name" is different from "vector name", take attention about that expecially when you use operations/selections on a data struttucre regardless if it's a sinple vector or a more complicated da structure.
> VectorOne<-c(9,3,5) > VectorOne [1] 9 3 5
It says a vector called "VectorOne" has 3 elements starting at elements [1], why we should know that it starts at element [1]? this feature is important when we work with a bigger dimension of our vector, infact it says the position n of the first element in the R-Console,for example if we have the following situation:
[1] 9 3 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 33 5
[29] 1 1 1
In the second line of the R-Console we have the first 1 is in the Position 29, and that's is very usefull when we have to select a single value from the position n of a big dimension data structure,but remember this change everytime we change R console window’s dimension and lauch again the last command.
After spent a bit of time about vector definition let's start watching some simple operation on single vector
Selecting Values
-Selection of a single value in a determinate position is very simple, all we have to do is write the position in square brackets right after the Vector Name.
"Vector Name"["Element in Position n"]
Example:
Select the 2° position in the vector specificated above.
Solution:
> VectorOne<-c(9,3,5) > VectorOne[2] [1] 3
-Selection of a multiple values in different vector positions is very simple as well, the difference from the single selection is that now we use a vector to specify the realltive positions we are interessed of and the syntax is just:
"Vector Name"["Vector Positions"]
Example:
Select the first and the last position in the vector specificated above
Solution
> VectorOne<-c(9,3,5) > VectorOne[c(1,3)] [1] 9 5
-Selection values from the position x to the position y use the square brackets with the relative positions separed by ":".
"Vector Name"["Position From":"Position To"]
Example:
Select the first and the second position in the VectorOne
Solution
> VectorOne<-c(9,3,5) > VectorOne[1:2] [1] 9 3
-Selecting and Repeating a single element, here we use a custom R function called rep("Position ","N° Times") that repeat the value a "N° of Times" selected from the "Position n" of the data structures.
Example:
Select the value in position 2 in the VectorOne and repeat it for 5 times
Solution
> VectorOne<-c(9,3,5) > VectorOne[rep(2,5)] [1] 3 3 3 3 3
Selecting Positions
After selecting the value from specificated query we could be interessed to know for example which vector position has the max or min value compared with the other positions, or which one has a bigger or lower value.
For this and other purposes we could use the which statement, we could just write some operations to let you understand the way it could be used.
-Selection of the position with a determinate condition, to determinate which position has a determinate condition we use the “which” constructor and it’s used in this way:
which(“Vector Name & Condition”)
Diffrent is the case when we want to apply an operation to the which statement fo find for example the position where there's the lowest or bigger value, in this case the syntax is a bit different and it's:
which."Type of operation to do"(“Vector Name ")
So instead of specifing all the combination we can use with R to extrapoly the position under a determinate condition, i just write some examples to let you understand how it can be used and changed in functuion of your needs.
Example:
Find the vector position where the values are greater then or equal to 5.
Find the vector position where the values are greater then or equal to 5.
Solution:
> VectorOne<-c(9,3,5) > which(VectorOne>=5) [1] 1 3
Some Vector Operations:
After I’ve been concetrated on what and the value we can select from our vector, let's start to see some operations we can use on it and i will put an example for every single operation.
min: Find the value in the vector that has the minimal value min("Vector Name")
max: Find the value in the vector that has the maximal value max("Vector Name")
length: Write the vector length in elements/positions length("Vector Name")
mean: Write the mean value of the vector values mean("Vector Name")
median:Write the median value of the vector values median("Vector Name")
range:Write the min and max value of the vector values range("Vector Name")
standard deviation: Point dispersion from the mean value sd("Vector Name")
variance:how far a set of number is from the mean value var("Vector Name")
or sum(("vector name")-mean("vectorname"))^2 / (length("vector name")-1)
ascending order: sort in ascending manner sort("Vector Name")
descending order: sort in descending order sort("Vector Name",decreasing=TRUE)



Nessun commento:
Posta un commento