Friday, June 13, 2014

Introduction to R: Vectors, Matrices and Lists

Our previous discussion talks about a short introduction to R, in this topic we will discuss the basic commands on creating vectors,matrices , and lists. To begin with, the syntax typed into the R console is termed as an expression and the "<-" symbol used in writing an expression is termed as the assignment operator, this gives a variable an assigned "value".
Example:
type the expression x <-5 in your R console, press enter, type x and press enter again. it will show you the value of x. What you did was entering an expression in R.





















In our example, the expression x<-5 is a numeric vector with a first element that is a number object 1. our second example is a character vector with a character string My name is Gerard.

Now that you have written your first expression, let's make a sequence of numbers. A number sequence can be created by the colon : operator. Type the the variable x in your R console and assign the value 1:30, then auto-print your expression by entering x. your expression should look like:





















The command expresses a sequence of numbers from 1 to 30. The output is an integer vector, none-scalar numbers. The first line has the [1] because the first element is the number 1 and the second line has the [26] because the first element starts with the number 26.

Now we will create vector of objects by the c() function. the c here refers to catenation, this means that it connects a series of objects to form ties or links.





















the first example, x<-c("1-2i", "1+i"), is termed as a complex vector because it has the complex, none real number, i.
The second example, x<-c("a","b","c"), is termed as a character vector because it has the characters in the alphabet.
the third and fourth examples, x<-c("TRUE", "FALSE") and x<-c("YES", "NO"), are termed logical vectors because it contains a priori conditions before an action will be executed. We will discuss logical functions in our advance topics.
The fifth example, x<-c(0.099, 1) is a numeric vector because it contains the number with scalar values.

We can also create a vector by the vector() function, it is actually the long hand way of writing a vector in R.





















Here we have an expression x<-vector("numeric", length=5), or a vector that contains numeric elements with a length of 5 objects. the output are all zero's because by default an unassigned numeric vector will have a value of 0.

Most of the time we express vectors with mixed objects. The R program prioritizes the vectors depending on the atomic classes present as an element of the vector. the sequence of prioritization are as follows:
1st Priority = character
2nd Priority = numeric
3rd Priority = logical
here is an example:





















In the first example, x<-c(2,"b"), the expression is a character vector and the number 2 is coerced as a character vector because of the element b which is under the atomic class of "character". Thus, this expression is a character vector with the elements 2 and b. The second example, x<-c("FALSE", 10) is a numeric vector with the elements "FALSE" and 10. The third example, x<-c("FALSE", "b") is a character vector with the elements "FALSE" and "b".

In R Language, you can express one atomic class element into another element by forced coercion using the as. function.





















In our example we created a sequence of numbers from 0 to 3 by the expression x<-0:3, to determine what kind of atomic class it is we used the class() function and typed class(x). The output showed that x is an integer vector, we then tried to force coerced the integer vector into numeric, logical, character and complex using the as. function. Take note that in the logical vector, by default the value 0 is equivalent to FALSE, anything greater than 0 is TRUE.

Note should be taken in forced coercing atomic classes to another atomic classes as there are times that it might end up as a "illogical coercion" which will result in NA. In the next example we have a character vector expression x<-c("a", "b", "c") and we try to express it as numeric, logical, integer and complex. The outputs are all NA because there is no logical way of forcefully expressing "a", "b" and "c" into another set of atomic classes.





















Now that you have made vectors, our next step is to create matrices. Matrices are special kind of vectors because it contains a dimension attribute. The dimension attribute in a matrix is defined by rows and columns (nrow, ncol).

Try typing in your R console x<-matrix(nrow=3, ncol=5) and hit enter. The output is a matrix with 3 rows and 5 columns. Try typing dim(x), and hit enter the output will show 3 and 5. 3 for rows and 5 for columns. Now type attribute(x) and hit enter and the output will give you the dimension of your matrix which is 3x5.





















Take note that matrices are created in a column-wise manner. This means that the first column is filled first and when the maximum number of rows is reached the next column is then filled. Say for example we type the expression x<-matrix(1:10, nrow=2, ncol=5).





















The maximum number of rows in our expression is two, as you have observed that the first column [,1] is filled first and when the maximum number of rows are reached, [1,] and [2,], the next column [,2] is then filled and so on.

Matrices can also be created by its dimension function, dim(). Lets create a series of numbers from 1 to 10, then lets create a 2x5 matrix (a matrix with 2 rows and 5 column) from this series of numbers using the dim() function.





















Matrices can also be made by cbind or rbind, this creates a matrix by binding rows and columns. If you want your vectors, say x and y, to be a part of the column, the cbind function cbin() will be used while if you want your vectors as a part of the rows then the rbind function rbind() is used.





















Aside from matrices, a list is also a special kind of vector that can be used in R. List are special because it contain different sets of atomic classes. This special kind of vector uses the list function, list(). Say for example you want to create a list x<-list("1+i", 3, "FALSE", "a"). This is a list containing a complex atomic class, a numeric, a logical and a character. The output is different because each element in the list has a different atomic class.

No comments:

Post a Comment