Tuesday, 31 March 2015

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

Is this tutorial useful for you?