**We will learn:**1. Vector Math (Using Scalar Value in

__Vector)__

**c**2. Scatter Plots

3. N/A Values

**Details:**###
**E. Vector Math**

Now we will try to add *scalar*value in

__vector.__

**c****What is Scalar?**

*. In Arithmetic Operation, you will find this math operation;*

**Scalar**is**a single**and**real value**
x =

**1**+**1**
y =

**4**/**2**
... etc.

Other

*samples*;
24 m ->

*Value***Scalar**
Note: For my math teacher, Drs.Sutikno, in SMA Negeri 1 Bukit Kemuning, North Lampung, Lampung Province (

in __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)."*, like our previous lesson, we have:*

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

Then, we add "2" to each values in

__vectors, as in:__

**c**> 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

__vector value after the previous one > a <- c(3, 9, 7), as in:__

**c**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

**) + 2 (in**

*a***)**

*b*9 (in

*) + 1 (in*

**a***)*

**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

__vector in previous__

**c***to*

**a***other*

*new*vector values:

**a***For example:*

previous

*vector: > a <- c(3, 9, 7)*

**a**new

*vector: > a <- c(1, 9, 7)*

**a**> a == c(1, 9, 7)

Result: [1] FALSE TRUE TRUE

then

*does not sum up both vectors, but index*

**R***of both vectors.*

**each values**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***or*

**Cos***then*

**Tan***will figure each values of*

**R***vector*

**a***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 can be provided.R object with a
`plot` method |

`y` |
the coordinates of points in the plot, yoptional
if `x` is an appropriate structure.(R-Documentation) |

By using

**plot()**we can

*by relating*

**draw a graph***to*

**x***coordinates.*

**y***, we then need*

**To draw it**__. In this case, the data must contains data__

**data***coordinate and*

**x***coordinates.*

**y***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!*

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:

###
**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

__vector, we assign that the third value is__

**c***NA*status. When we need the

*result of*,

**a**vector*will give you N/A or NA, as in:*

**R**> sum(a)

Result: [1] NA

In this case, we use

*function to test it.*

**sum***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

*, as in:*

**TRUE**> 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*