How can R be used by them easily. The next series of blogs will be on R.
The contents are available in a book form, leave your email in comments and I will mail the book to you. (PDF)
=========================================================================
Chapter 1 Vectors
2.1 Introduction
> a<-10 span="">-10>
> b<-c span="">-c>
> a
[1] 10
> b
[1] 10
> class(a)
[1] "numeric"
> class(b)
[1] "numeric"
|
Vector a is
defined as a number, where as b is defined as an array. But both are treated
as an array of one number.
The class()
function determines type of the array.
<- a="" and="" assign="" hyphen.="" is="" less="" operator="" span="" than="" the="" to="" value="">->
|
> a[1]
[1] 10
> b[1]
[1] 10
|
As a proof
that both are the same description we can try to print the first element in
the array. Note a also behaves like an array because it is one.
|
2.2 Operations in R.
> a<-10 b="" c="" span="">-10>
> a;b
[1] 10
[1] 10 20 30
> a+10;a*10;a^2;a%%3
[1] 20
[1] 100
[1] 100
[1] 1
> b+10;b*10;b^^2;b%%3
[1] 20 30 40
[1] 100 200 300
> b+10;b*10;b^2;b%%3
[1] 20 30 40
[1] 100 200 300
[1] 100 400 900
[1] 1 2 0
|
The ; can be
used to separate multiple commands to be issued in the same row.
The arithmetic
operators +, - * , / are self explanatory. ^ is for the power of.
a^2 here means
a2
%% is the
modulus operator, it gives the remainder.
|
> a>10;b>10
[1] FALSE
[1] FALSE
TRUE TRUE
|
Logical
operators are supported as well. >, >=, <. <=, !=.
The logical
operators return a logical output.
|
2.3 Name in R.
> sales<-c span="">-c>
> sales
[1] 120 190 90
> sales[1]
[1] 120
> sales[3]
[1] 90
|
Here the sales
vector is defined with three numbers. You can check by entering class(sales)
and you get an output NUMERIC.
The elements
are accessed using numbers 1,2… etc.
|
> names(sales)<-c an="" ar="" eb="" span="">-c>
> sales
Jan Feb Mar
120 190 90
|
The names
function gives names to the columns. Since we have three columns in the sales
vector, we give three names. (Jan, Feb and Mar).
|
> max(sales)
[1] 190
>
> sales==max(sales)
Jan Feb
Mar
FALSE TRUE FALSE
|
The max is an
inbuilt function that goes thru the vector (sales) and lists the column that
has the max value.
When you list
the sales vector with sales==max(sales), it gives us a logical vector with
TRUE/FALSE values.
|
> sales[max.sales]
Feb
190
> class(sales)
[1] "numeric"
> class(max.sales)
[1] "logical"
|
Here we create
a new logical vector called max.sales. Remember that vector names can have
. or _ or lower/upper case characters.
The
sales[max.sales] is used to display that column that has the TRUE value.
|


