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:

Friday 3 April 2015

Learn Vectors 2


We will learn:

1. Vector Names
2. Plotting One Vectors

Details:

This is part two of learning vectors in R programming language. If you haven't learned about the first part, you can start to learn vectors part one.

D. Vector Names


For instance, I have three values (e.g. red, yellow and black) which is stored in wordlist c vector. That will be written as follows;

wordlist <- c("red","yellow","black")    # wordlist stored "red", "yellow" and "black" value.

As explained in Vector Access, when you need to pick one of the values, you can do like this:

rank[1:3]
Result: [1] "red","yellow","black"

Now, this time we will learn that "we can assign names to a vector's elements by passing a second vector filled with names to the "names" assignment function".(taken from. http://tryr.codeschool.com/levels/2/challenges/18) , as in:

names(wordlist) <- c("red","yellow","black")

(Note: to see more about names function in R, type:
help(names)
)

When we want to access or pick one of the values, we can type like this:

wordlist["yellow"]
Result: yellow
      2

From the example above, that means, that we can access the value directly beside our common way, as in:

wordlist[2]

Result: yellow
      2

From here, we also can change the position of "yellow" (that is: 2) to other position, for example, 7.

wordlist["yellow] <- 7

Now, see if it already changes:

wordlist["yellow"]
Result: "7"

or, with other way;

wordlist[7]
Result: "yellow"

E. Plotting One Vector


This section, we will learn about barplot. What is it? barplot is R function which is used to create vertical or horizontal bars (e.g. chart) with vector values. For further and details about that, just ask R to answer by typing: -

help(barplot)

Hint: Talking about Vectors will force you to remember 2 things: Variables and Values.

For example, we have ( again and again ... :p ) three values in c vector, they are: 3, 5 and 1, as follows:
1. Variable : stuDentS (warning: case sensitive!)
2. Value:  3, 5, 1.

in R, as usual, you can type:

stuDentS<- c(3,5,1)

Then, let's start using barplot (hurrayyyy):

barplot(stuDentS)

Done! R will give us this amazing result!!!


Barplot in R Programming


Further, if you assign new names in the vector values (... say, barney, herman and adhya), R will take it as Labels which will be placed below each bars based on the order you give.
For example:

names(stuDentS)<-c("Aminah","Herman","Adhya")

Then, R will give you this awesome chart!


Barplot in R Programming


Now, to show other types of chart, you can give range from 1 through 100, as in:

barplot(1:100)

Here's the result:




See you on the next lesson :D, my best regards.

Labels:

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"


Labels:

Introduction to R



We will learn:

1. Expression
2. Logical Value
3. Variables
4. Functions
5. Help
6. Files

Details:

This following lesson is taken from http://tryr.codeschool.com/ . As a beginner, I finally found a place to practice R excellently (which is recommended by RStudio. Thanks, bro - I also recommend you to start your R days in that amazing site.

Here, we will learn some simple things of R commands and execution including, Expression, Logical Values, Variables, Functions, Helps and Files.

Warning! R is Case Sensitive!

A. Expression


a.1. Simple Math Operation. When we would like to get result from  1+1, then we don't need equals sign, as follows:

> 1+1
Result: [1] 2

we can do the same with minus [-], multiplying [*] and devision [/]. See this following examples:

Minus
> 9 -3
Result: [1] 6

Multiplying
> 4*2
Result: [1] 8

Division
> 10/2
Result: [1] 5

a.2. String Value. R takes string value written with Quotes, as follows:

> "Hi, Everyone!"
Result: [1] "Hi, Everyone!"

B. Logical Value

Talking about Logical value means talking about TRUE or FALSE. R has it as well as other programming languages do. Let's start!

b.1. To check if a statement is TRUE or FALSE (TRUE or FALSE written in uppercase. Remember! It's a case sensitive).


Less than <
> 6<9
Result: [1] TRUE

More than >
> 4>1
Result: [1] TRUE

Equals (Use double-equal-sign to do that!)
6+4 == 12
Result: [1] FALSE

T and F (TRUE and FALSE shorthand)
F==FALSE
Result: [1] TRUE

C. Variables ("I love this stuff in all programming language")

As you know that variables are used to store values. That stored values will be accessed later.
Let's start!

c.1. Math Variable. Here we will use a variable to store and, at last, we will do with simple math operation.
Examples:

> x<-50
> x/2
Result: [1] 25

c.2. String Variable. Here, we store string in /z/ and execute it.
Example:

> z<- "Welcome!"
> z
Result: [1] "Welcome!"

c.3. Logical Value.
Example:

> x<-FALSE
Result: [1] FALSE

D. Functions

Time to more complicated :D ... Here's we will do some Math Functions, such as, Sum, Average, etc.
Lets' start!

d.1. Sum (a comma means 'Plus')
> sum(2,5,3)
Result: [1] 10

d.2. Repeat a value (we use rep with times=...)
Formula:
rep(string-value,times=numeric)

Example:
> rep("Go!",times=4)
Result: [1] "Go!" "Go!" "Go!" "Go!"

d.3. Square Root Operation
Example:

> sqrt(16)
Result [1] 4

E. Help

How to get Help in R ?
Lets' start!

e.1. For global help, we type:
> help()

e.2. For specific help, type the specific word inside the braces, as follows:
> help(c)

Another example:
> help(sum)

Note: when you need to know how /sum/ or /c/ works in an example, then you type as follows:
> example(sum)

or,
> example(c)

Another example:
> example (rep)

R then gives you some examples about how to use them

F. Files

When you have files listed (R Projects) in your directory (R's files has dot+R in its extension, e.g. myfile.R), you can list of them by typing list.files() functions, as follows:

> list.files()

Let's say that one of the files listed is: myfile.R. Now, run the files with:

> source("myfile.R")

Warning! do not forget to use quotes inside the braces.
Well, that's all for the start since the tutorial I've got stop at that "F" too. :D

Labels: