Friday, 3 April 2015

Learn Vectors 2

We will learn:

1. Vector Names
2. Plotting One Vectors


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:

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. , as in:

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

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

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

Result: yellow

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


Result: yellow

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:

Result: "7"

or, with other way;

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


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):


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:


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:


Here's the result:

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

Tuesday, 31 March 2015

Learn Vectors

We will learn:

1. Vectors
2. Sequence Vectors
3. Vector Access


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:

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

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

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.


Then, show it by typing as follows:

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

Introduction to R

We will learn:

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


This following lesson is taken from . 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:

> 9 -3
Result: [1] 6

> 4*2
Result: [1] 8

> 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)
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.

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

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

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

c.3. Logical Value.

> 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=...)

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

d.3. Square Root Operation

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

> 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

Is this tutorial useful for you?