Monday, 6 April 2015

Learn Vectors 3


We will learn:

1. Vector Math (Using Scalar Value in c Vector)
2. Scatter Plots
3. N/A Values

Details:

E. Vector Math

Now we will try to add scalar value in c vector. What is Scalar? Scalar is a single and real value. In Arithmetic Operation, you will find this math operation;


x = 1 + 1
y = 4 / 2
... etc.

Vector Math in R Programming | Pemrograman R

Other samples;

24 m -> Scalar Value
24 m2 -> Vector Value


 

Note: For my math teacher, Drs.Sutikno, in SMA Negeri 1 Bukit Kemuning, North Lampung, Lampung Province (studied in 1992) : "This is the first time for me to understand what Scalar & Vector is. ..... - I understand this when I understand English Language ... (crying)."




in R, like our previous lesson, we have:

> a <- c(3, 9, 7)

Then, we add "2" to each values in c vectors, as in:

> a + 2
Result: [1] 5    11    9

This is simple example where a is 3, a is 9 and a is 7. Each of the a(s) is counted up to 2 (Scalar).

Further, you can do that with other arithmetic operation, as follows:

> a * 3
Result: [1]  9   27    21

> a / 3
Result: [1] 1.000000     3.000000     2.333333

Now, if we have other c vector value after the previous one > a <- c(3, 9, 7), as in:

b <- c(2,1,4)

and, add them (a + b) up, that will be as follows:

> b <- c(2,1,4)
> a + b
Result:  [1]  5    10    11

The result is coming from:

3 (in a) + 2 (in b)
9 (in a) + 1 (in b)
7 (in a) + 4 (in b)

Try other operation by substract b - a or vice verse!
.......
Take notice that, when you try to compare c vector in previous a to other new a vector values:

For example:

previous a vector: > a <- c(3, 9, 7)
new a vector: > a <- c(1, 9, 7)

> a == c(1, 9, 7)
Result:  [1] FALSE   TRUE   TRUE

then R does not sum up both vectors, but index  each values of both vectors.
Now we try to use > (more than) to compare each values in a and b vectors, as follows:

> a > c (1,9,7)
Result: [1] TRUE   FALSE   FALSE

Vectors in Trigonometric Function

When you use Trigonometric Function, such as, Sin, Cos or Tan then R will figure each values of a vector against Sin, Cos or Tan,  as follows:

> sin(a)
Result: [1]   0.1411200 0.4121185 0.6569866

> cos(a)
Result: [1] -0.9899925 -0.9111303  0.7539023

> tan(a)
Result: [1] -0.1425465 -0.4523157  0.8714480

Now, using sqrt function in a vectors:

> sqrt(a)
Result: [1]   1.732051 3.000000 2.645751

F. Scatter Plots

In R, Plot function handles graphic as written below:

> plot(x,y)

x the coordinates of points in the plot. Alternatively, a single plotting structure, function or any R  object with a plot method can be provided.
y the y coordinates of points in the plot, optional if x is an appropriate structure.
(R-Documentation)

By using plot() we can draw a graph by relating x to y coordinates. To draw it, we then need data. In this case, the data must contains data x coordinate and y coordinates.

For example:

> x <- seq(1, 20, 0.1)
> y <- sin(x)
(taken from: http://tryr.codeschool.com/levels/2/challenges/35)

Then we do the plot on both data, as follows:

> plot(x,y)
Result:               # ........ see this following graph, awesome!


Scatter Plots in R Programming | Pemrograman R


Now, let's take another example by using negative values in one of the vectors values and assign the first vector into absolute function in second vector, as follows:

Data

1. First vector values (using negative values)
> mylesson <- -2:7
2. Second vector values (using negative values)
 > mygrade <- abs(mylesson)

> plot(mylesson,mygrade)
Result:

Scatter Plots in R Programming | Pemrograman R


G. N/A Values

In a sample of a vector, one of the values is not available. This sometimes occurs in a column in Database where the data in that column is not filled by user for optional form-sheet. In database, we usually set it active as NULL data.
In R, this NULL data means not exists or not available, then R assigns it as Non Active or N/A Values.

For example:

> a <- c(1, 3, NA, 7, 9)

,where within c vector, we assign that the third value is NA status. When we need the result of a vector, R will give you N/A or NA, as in:

> sum(a)
Result: [1] NA

In this case, we use sum function to test it. sum function considers it as NA since the calculation is not complete yet (NA means: can not be calculated). see the sum documentation in R by typing help(sum).
"As you see in the documentation, sum can take an optional named argument, na.rm. It's set to FALSE by default, but if you set it to TRUE, all NA arguments will be removed from the vector before the calculation is performed." (http://tryr.codeschool.com/levels/2/challenges/38)
However, R can ignore the NA values by calling na.rm and set it to TRUE, as in:

> sum(a,na.rm=TRUE)
Result: [1] 20 #where 20 is coming from: 1+3+7+9.




See you on the next lesson!
My best regards


Labels: