Tuesday, 31 March 2015

Learn Vectors


We will learn:

1. Vectors
2. Sequence Vectors
3. Vector Access

Details:

In R Programming, R handles many data types; such as, scalars, vectors (numerical, character/strings, logical), data frames, matrices, and lists. (as we learn in Introduction to R Programming Language For Beginner page). However, in this chance, we'll discuss about Vectors.

A. Vectors


When using vectors in R, we need to have the same data types, as follows:

> c(1,9,5)
Result: [1] 1 9 5

From the example above, we see that 1, 9 and 5 has the same data type. Note that c is short  for word "combine". It combines (the same) values inside c's braces.

When c has different values of data types, it would shows all in strings data type. See below!

> c(7,"Hello",FALSE)
Result: [1] "7"  "Hello"  "FALSE"

Now, let's go more details about Vectors.

B. Sequence Vectors


Vectors also handles sequence values as in c functions. e.g. c(1,2,3,4,5).
The sequence value vector can be written as follows:

> c(5:7)
Result: [1] 5 6 7

That means, when you write: 5:7, R will give the output 5 6 and 7. This is the way for you to order numbers in R. Other method to call the data inside braces which has the same function as in c, namely, seq.
For example:

> seq(5:7)
Result: [1] 5 6 7

When you input values that less then 1 in the back of that value, e.g. seq(5,7, 0.5), etc., the last value (0.5) will be added up to each value:

> seq(5,7,0.5)
[1] 5.0  5.5  6.0  6.5  7.0

How is it done?
See this following illustration!


From the illustration we can see where 5 must reach 7 by adding 0.5.







When the value has 7 to 5 as in:

7:5
or,
> seq(7:5)
Result: [1] 7   6   5

C. Vector Access


c.1. Store Variable inside Vector C
Now we will store vector value inside a variable. Let's take one variable, say, wordlist. Here, wordlist will store 3 values inside c vector.
Example:

> wordlist <- c('I', 'learn', 'R-language')

If we order (as well as R interprets) the value, that will be like this:

I = 1
learn = 2
R-language = 3

Then, we try to pick number 2 of the values.
Type like this!

> wordlist [2]

Then, R will give you the result:

[1] "learn"

From the example above we can see that the variable wordlist stores c vector value; I, learn and R-language. In R, the three will be ordered into 1, 2 and 3.

Warning! Array in R starts from 1 NOT 0 (zero)!

c.2. Change Value

Now, lets change one of the values stored in c vector. As we do above, the second value (no.2) is the word learn. Try to change it to love, as in:

> wordlist[2] <- 'love'

Then, pick the number to show it has successfully changed by typing as follows:

> wordlist[2]

R will give you the result: [1] "love"

c.3. Add New Value

Type as follows:
> wordlist[4] <- 'badly'

This is the way if you need to add new value and store it in wordlist variable. So, there are 4 values now; I, love, R-language and badly.

c.4. Access more than one Value.

If you want to access the stored variable and pick two or more value to show, then use Square Brackets / [ .......] where c vector within it.
Example:

wordlist [ c (2,4) ]
Result: [1] "love"   "badly"

c.5. Access more than one value in a certain range.

From the discussion above, we already have 4 values; I, love, R-language, badly. If we need to show more than one value in a range, say, 2 to 4 or love through badly, then you can type as follows:

> wordlist [2:4]
Result: [1] "love"   "R-language"   "badly"

Note that the range value is inside square brackets!

c.6. Add more than one value inside c vector

If you want to add more than one value and store them inside the variable wordlist, then just type it and R will do that for you :p
For example, we add three values at number 5,6 and 7.

wordlist[5:7]<-c('and','you','do')

Then, show it by typing as follows:

> wordlist [6]
Result: [1] "you"


Is this tutorial useful for you?