R language


Basic R commands

Hi folks, Today we going to learn about R Language and its basic commands.
R is a programming language and free software environment for statistical computing and graphics that is supported by the R Foundation for Statistical Computing. The R language is widely used among statisticians and data miners for developing statistical software and data analysis.

Write these small script into rscript (New File -> Rscript).After write every script hit ctrl+Enter then your script will be run.
1+1
output will show in console window like this
> 1+1
[1] 2

R commands
CommandOutput
1-1 > 1-1
[1] 0
6/2 > 6/2
[1] 3
pi^2 > pi^2
[1] 9.869604
exp(1) > exp(1)
[1] 2.718282
var1 <- pi > var1 <- pi
var1^2 > var1^2
[1] 9.869604
var2 = 42 > var2 = 42
exp(3) -> var3 > exp(3) -> var3
> var3 
 [1] 20.08554
Defining Vector
vec1 <- c(1,2,3) > vec1 <- c(1,2,3) > vec1 [1] 1 2 3
vec2 <- 1:11 > vec2 <- 1:11 > vec2  [1]  1  2  3  4  5  6  7  8  9 10 11
vec3 <- rep(x=4,7)
 (rep stands for repeat some certain number)
> vec3 <- rep(x=4,7) > vec3 [1] 4 4 4 4 4 4 4
vec4 <- seq(from=1,to=12,by=1.333)
(sequence number vector. by stands for increment by that value)
> vec4 <- seq(from=1,to=12,by=1.333) > vec4 [1]  1.000  2.333  3.666  4.999  6.332  7.665  8.998 10.331 11.664
vec5 <- seq(1,12,length.out =10 ) > vec5 <- seq(1,12,length.out =10 ) > vec5  [1]  1.000000  2.222222  3.444444  4.666667  5.888889  7.111111  8.333333  9.555556 10.777778 [10] 12.000000
Indexing Vector(Same kind of element)
vec4[3]
(Access 3rd element of vec4)
> vec4[3] [1] 3.666
vec4[1:3]
(Access to st to 3rd element)
> vec4[1:3] [1] 1.000 2.333 3.666
vec4[23:24] > vec4[23:24] [1] NA NA
vec4[c(1,3,7)]
(Access specific index element )
> vec4[c(1,3,7)] [1] 1.000 3.666 8.998
vec4[c(1,1,1,2)] > vec4[c(1,1,1,2)] [1] 1.000 1.000 1.000 2.333
vec4[-4]
(access except element index number 4)
> vec4[-4] [1]  1.000  2.333  3.666  6.332  7.665  8.998 10.331 11.664
vec4[-(1:3)]
(access except index number 1 to 3)
> vec4[-(1:3)] [1]  4.999  6.332  7.665  8.998 10.331 11.664
vec4[-c(4,5,7)]
(access except index number 4 ,5 and 7)
> vec4[-c(4,5,7)] [1]  1.000  2.333  3.666  7.665 10.331 11.664
vec4[9:4]
(Access element reverse order)
> vec4[9:4] [1] 11.664 10.331  8.998  7.665  6.332  4.999
Relational Operator
vec5 <- c(2,2,2)
(Defining vector)
> vec5 <- c(2,2,2) > vec5 [1] 2 2 2
all.equal(vec5,c(2,1,2)) > all.equal(vec5,c(2,1,2)) [1] "Mean relative difference: 0.5"
all.equal(vec5,c(2,2,2)) > all.equal(vec5,c(2,2,2)) [1] TRUE
vec5==c(2,2,2) > vec5==c(2,2,2) [1] TRUE TRUE TRUE
vec5>c(2,2,2) > vec5>c(2,2,2) [1] FALSE FALSE FALSE
vec5>c(1,2,3) > vec5>c(1,2,3) [1]  TRUE FALSE FALSE
vec5<=c(1,2,3) > vec5<=c(1,2,3) [1] FALSE  TRUE  TRUE
vec5!=c(1,2,3) > vec5!=c(1,2,3) [1]  TRUE FALSE  TRUE
List Operator(It can be anything)
list1 <- list(vec1,vec2,vec4) > list1 <- list(vec1,vec2,vec4) > list1 [[1]] [1] 1 2 3 [[2]]  [1]  1  2  3  4  5  6  7  8  9 10 11 [[3]] [1]  1.000  2.333  3.666  4.999  6.332  7.665  8.998 10.331 11.664
list2 <-list(booyah=vec1,boombody=vec2,bam=vec3) > list2 <-list(booyah=vec1,boombody=vec2,bam=vec3) > list2 $booyah [1] 1 2 3 $boombody  [1]  1  2  3  4  5  6  7  8  9 10 11 $bam [1] 4 4 4 4 4 4 4
list2$booyah
($ sign stands foraccessing name)
> list2$booyah [1] 1 2 3
listvar <- list2$bam
(Redefine a list)
> listvar <- list2$bam > listvar [1] 4 4 4 4 4 4 4
list2$bam > list2$bam [1] 4 4 4 4 4 4 4
listvar <-list2$bam[1:2]> listvar <-list2$bam[1:2] > listvar [1] 4 4

Data Visualization.


R commands
CommandOutput
data(iris) > data(iris)
summary(iris)
sl <- iris$Sepal.Length > sl <- iris$Sepal.Length
hist(sl)  > hist(sl)
density(sl)
sl.d <- density(sl) > sl.d <- density(sl)
plot(sl.d) > plot(sl.d)


?hist It will show help with regarding function
hist(sl,freq =F)
(change the frequency as probability.)
lines(sl.d)
> hist(sl,freq =F)
> lines(sl.d)

boxplot(sl) > boxplot(sl)

sl.b <- boxplot(sl)
(Define the boxplot)
> sl.b <- boxplot(sl)
summary(sl.b)
(Summarize the boxplot)
> summary(sl.b)
      Length Class  Mode   
stats 5      -none- numeric
n     1      -none- numeric
conf  2      -none- numeric
out   0      -none- numeric
group 0      -none- numeric
names 1      -none- character
names(sl.b)
(a vector of names for the groups.)
> names(sl.b)
[1] "stats" "n"     "conf"  "out"   "group" "names
sl.b$stats
(Stats indicates a matrix, each column contains
  1. the extreme of the lower whisker
  2. the lower hinge,
  3. the median
  4. the upper hinge
  5. extreme of the upper whisker for one group/plot. )
> sl.b$stats
     [,1]

[1,]  4.3
[2,]  5.1
[3,]  5.8
[4,]  6.4
[5,]  7.9
sl.b$stats[3]
(Access to matrix index(median))
> sl.b$stats[3]
[1] 5.8
barplot(sl) > barplot(sl)

pairs(iris[,1:4])
(Access specific index element )
> pairs(iris[,1:4])

qqnorm(sl)
(testing Normalities)
> qqnorm(sl)


qqline(sl)
(testing Normalities)
> qqline(sl)

par(mfrow = c(2,2))
(Partiion the graphical view of display 
(mfrow=c(nr, nc))it indicate howmany row and coloumn your partition would be)

(Then run whole plot as one command using select all and hit enter wile pressing ctrl)

hist(sl,freq =F)
lines(sl.d)
boxplot(sl)
qqnorm(sl)
qqline(sl)
barplot(sl)
> par(mfrow = c(2,2))
> hist(sl,freq =F)
> lines(sl.d)
> boxplot(sl)
> qqnorm(sl)
> qqline(sl)
> barplot(sl)


library("lattice")

(Load the library lattice)
bwplot(Sepal.Length~Sepal.Width,data=iris)
> library("lattice")
> bwplot(Sepal.Length~Sepal.Width,data = iris)
















(Each Sepal width has each Sepal length plot)
dotplot(Sepal.Length~Sepal.Width,data=iris)
(Access element reverse order)
> dotplot(Sepal.Length~Sepal.Width,data = iris)

(Another way to see the values)

xyplot(Sepal.Length~Sepal.Width |Species,data=iris) 
(Analysis the data with specific frequency range)
> xyplot(Sepal.Length~Sepal.Width | Species,data = iris)


library("psych")
(Load the library "psych" if it not in Rstudio have to installed it)
pairs.panels(iris)
> library("psych")
> pairs.panels(iris)
source("myplot.R")
(Load the R extension file)
> source("myplot.R")

Comments

Popular posts from this blog

Easy understanding of MVC design architecture

A / B Testing

Firewall