We will learn:
1. Vector Math (Using Scalar Value in c Vector)
3. N/A Values
E. Vector MathNow 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;
> a <- c(3, 9, 7)
Then, we add "2" to each values in c vectors, as in:
> a + 2
Result:  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:  9 27 21
> a / 3
Result:  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:  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:
previous a vector: > a <- c(3, 9, 7)
new a vector: > a <- c(1, 9, 7)
> a == c(1, 9, 7)
Result:  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:  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:
Result:  0.1411200 0.4121185 0.6569866
Result:  -0.9899925 -0.9111303 0.7539023
Result:  -0.1425465 -0.4523157 0.8714480
Now, using sqrt function in a vectors:
Result:  1.732051 3.000000 2.645751
F. Scatter PlotsIn R, Plot function handles graphic as written below:
||the coordinates of points in the plot. Alternatively, a
single plotting structure, function or any R object with a
||the y coordinates of points in the plot, optional
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.
> 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:
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:
1. First vector values (using negative values)
> mylesson <- -2:7
2. Second vector values (using negative values)
> mygrade <- abs(mylesson)
G. N/A ValuesIn 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.
> 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:
Result:  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:
Result:  20 #where 20 is coming from: 1+3+7+9.
See you on the next lesson!
My best regards