7.pdf This presentation captures many uses and the significance of the number...
Basic and logical implementation of r language
1. R Language
INTRODUCTION:
R is an open source programming language and software environment for statistical computing
and graphics. The R language is widely used among statisticians and data miners for developing
statistical software and data analysis. But, we are going to integrate R with Hadoop in order to
manage BigData efficiently. Here, we have tried to figure out some basic commands of R
followed by logical implementation using R as well.
BASIC COMMANDS:
GetDirectory:
getwd() [ R language is case sensitive ]
Assignment:
Single value:
s < - 3 or s = 3 [The value of s is 3]
Multiple values:
s < - c (1, 2, 3) [The value of s is 1, 2, 3]
Or
s < - c (1:3) [The value of s is 1, 2, 3]
2. Mean:
mean(x) [The mean of x i.e. 1, 2, 3 is 2]
Variance:
var(x) [The variance of x i.e.1, 2, 3 is 1]
Linear Model:
lm_1 < - lm(y~x) [The linear model between two variables will be shown]
Graphical Representation:
plot (lm_1) [The linear model will be graphically represented]
Summary:
summary (lm_1) [The summary of the linear model will be shown]
List of variables:
ls () [ The list of all variables used will be shown]
Reading .csv files:
read.table ( file=”sample.csv”)
[Files can be read in this way, mostly csv files]
3. Reading .xls files:
install.packages("gdata") [ It can be done manually too. Go to Packages at the
top. Select any location, preferably USA (CA 1).
Then select gdata from the list. ]
library(gdata) [ Use of Library for reading an Excel file. ]
setwd("D:/R Statistics") [ Set working directory ]
y=read.xls("iris.xls") [Read the .xls/.xlsx file that is present in the working
directory. ]
y [ Print the contents of the excel file. ]
These were some basic knowledge on R. Now, some logical implementations are being laid
down below:
4. LOGICAL IMPLEMENTATION:
Conditional:
Example:
x=5 # Creates sample data
if(x!=5)
{
print(1)
} else
{
print(2)
}
*else should be printed after “}”, not in a new line.
Output:
[1] 2
Ifelse:
Ifelse statements operate on vectors of variable length.
Syntax:
ifelse(test, true_value, false_value)
5. Example:
x = 1:10 # Creates sample data
ifelse(x<5 | x>8, x, 0)
Output:
[1] 1 2 3 4 0 0 0 0 9 10
For loop:
Example:
x=5
for(i in seq(along=x))
{
if(x==5)
print(1)
else
print(2)
}
Output:
[1] 1
*The use of “along” prints the value for once only. At the same time, the absence of along
will print the value x times likewise;
7. Apply loop:
Example 1(Coloumwise operation):
x= matrix(c(1:9),3,3)
apply(x, 1, sum)
Output:
[1] 12 15 18
Example 2(Rowwise operation):
x= matrix(c(1:9),3,3)
apply(x, 2, sum)
Output:
[1] 6 15 24
lapply:
Applies a function to elements in a list or a vector and returns the results in a list.
Exmple:
# create a list with 2 elements
l = list(a = 1:10, b = 11:20)
# the mean of the values in each element
lapply(l, mean)
Output:
$a
[1] 5.5
$b
[1] 15.5
8. # the sum of the values in each element
lapply(l, sum)
Output:
$a
[1] 55
$b
[1] 155
sapply:
Exmple1:
li = list("klaus","martin","georg")
sapply(li,toupper)
Output:
[1] "KLAUS" "MARTIN" "GEORG"
Exmple2:
# create a list with 2 elements
l <- list(a = 1:10, b = 11:20)
# mean of values using sapply
l.mean <- sapply(l, mean)
# what type of object was returned?
class(l.mean)
[1] "numeric"
# it's a numeric vector, so we can get element "a" like this
l.mean[['a']]
Output:
[1] 5.5
9. vapply:
vapply is similar to sapply, but has a pre-specified type of return value, so it can be safer (and
sometimes faster) to use.
Exmple:
l <- list(a = 1:10, b = 11:20)
# fivenum of values using vapply
l.fivenum <- vapply(l, fivenum, c(Min.=0, "1st Qu."=0, Median=0, "3rd Qu."=0, Max.=0))
class(l.fivenum)
[1] "matrix"
# let's see it
l.fivenum
Output:
a b
Min. 1.0 11.0
1st Qu. 3.0 13.0
Median 5.5 15.5
3rd Qu. 8.0 18.0
Max. 10.0 20.0
REFERENCE:
[1] http://www.r-project.org
[2] http://www.cyclismo.org/tutorial/R/types.html
[3] http://manuals.bioinformatics.ucr.edu/home/programming-in-r
[4] http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r