R LanguageINTRODUCTION:R is an open source programming language and software environment for statistical computingand grap...
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...
Reading .xls files:install.packages("gdata")             [ It can be done manually too. Go to Packages at the             ...
LOGICAL IMPLEMENTATION:Conditional:Example:x=5            # Creates sample dataif(x!=5){    print(1)} else{    print(2)}*e...
Example:x = 1:10        # Creates sample dataifelse(x<5 | x>8, x, 0)Output:[1]        1 2 3 4 0 0 0 0 9 10For loop:Example...
x=5for(i in seq(x)){    if(x==5)           print(1)    else           print(2)}Output:[1]    1[1]    1[1]    1[1]    1[1] ...
Apply loop:Example 1(Coloumwise operation):x= matrix(c(1:9),3,3)apply(x, 1, sum)Output:[1] 12 15 18Example 2(Rowwise opera...
# the sum of the values in each elementlapply(l, sum)Output:$a[1] 55$b[1] 155sapply:Exmple1:li = list("klaus","martin","ge...
vapply:vapply is similar to sapply, but has a pre-specified type of return value, so it can be safer (andsometimes faster)...
Basic and logical implementation of r language
Upcoming SlideShare
Loading in …5
×

Basic and logical implementation of r language

1,861 views
1,767 views

Published on

Published in: Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,861
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Basic and logical implementation of r language

  1. 1. R LanguageINTRODUCTION:R is an open source programming language and software environment for statistical computingand graphics. The R language is widely used among statisticians and data miners for developingstatistical software and data analysis. But, we are going to integrate R with Hadoop in order tomanage BigData efficiently. Here, we have tried to figure out some basic commands of Rfollowed 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]Ors < - c (1:3) [The value of s is 1, 2, 3]
  2. 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. 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 laiddown below:
  4. 4. LOGICAL IMPLEMENTATION:Conditional:Example:x=5 # Creates sample dataif(x!=5){ print(1)} else{ print(2)}*else should be printed after “}”, not in a new line.Output:[1] 2Ifelse:Ifelse statements operate on vectors of variable length.Syntax:ifelse(test, true_value, false_value)
  5. 5. Example:x = 1:10 # Creates sample dataifelse(x<5 | x>8, x, 0)Output:[1] 1 2 3 4 0 0 0 0 9 10For loop:Example:x=5for(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 alongwill print the value x times likewise;
  6. 6. x=5for(i in seq(x)){ if(x==5) print(1) else print(2)}Output:[1] 1[1] 1[1] 1[1] 1[1] 1While Loop:Example:z=0while(z < 5){ z=z+2 print(z)}Output:[1] 2[1] 4[1] 6
  7. 7. Apply loop:Example 1(Coloumwise operation):x= matrix(c(1:9),3,3)apply(x, 1, sum)Output:[1] 12 15 18Example 2(Rowwise operation):x= matrix(c(1:9),3,3)apply(x, 2, sum)Output:[1] 6 15 24lapply:Applies a function to elements in a list or a vector and returns the results in a list.Exmple:# create a list with 2 elementsl = list(a = 1:10, b = 11:20)# the mean of the values in each elementlapply(l, mean)Output:$a[1] 5.5$b[1] 15.5
  8. 8. # the sum of the values in each elementlapply(l, sum)Output:$a[1] 55$b[1] 155sapply:Exmple1:li = list("klaus","martin","georg")sapply(li,toupper)Output:[1] "KLAUS" "MARTIN" "GEORG"Exmple2:# create a list with 2 elementsl <- list(a = 1:10, b = 11:20)# mean of values using sapplyl.mean <- sapply(l, mean)# what type of object was returned?class(l.mean)[1] "numeric"# its a numeric vector, so we can get element "a" like thisl.mean[[a]]Output:[1] 5.5
  9. 9. vapply:vapply is similar to sapply, but has a pre-specified type of return value, so it can be safer (andsometimes faster) to use.Exmple:l <- list(a = 1:10, b = 11:20)# fivenum of values using vapplyl.fivenum <- vapply(l, fivenum, c(Min.=0, "1st Qu."=0, Median=0, "3rd Qu."=0, Max.=0))class(l.fivenum)[1] "matrix"# lets see itl.fivenumOutput: a bMin. 1.0 11.01st Qu. 3.0 13.0Median 5.5 15.53rd Qu. 8.0 18.0Max. 10.0 20.0REFERENCE:[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

×