You are on page 1of 13

R Programming

Introduction of R
• Deals with statistical computation of data &
graphical representations.
• Developed at Bell laboratories, by John
Chambers & colleagues.
• Includes :
• Linear- nonlinear modelling
• Classical statistical test
• Time-series analysis, etc.
• Widely used by data scientists.
basic commands
Command Application Sample

X=12
1 Assign value
X<- 12

2 rm() To remove assigned variables rm(x)

3 ls() To view all assigned variables ls()

4 exp() Exponential function exp(2)

5 ^ square 24^4

6 sqrt() Square root sqrt(196)

7 abs() Absolute value abs(-14)

seq(12,40,3)
seq(from=a ,to= b,
8 sequence From 12 to 40 by increment
by=c)
of 3
Command Application Sample

Assign multiple values


9 X = c(a,b,c,d,e,f)
(numeric)

10 X = c(“abc”, “xyz”) Assign multiple values (string) X= c(“rahul”, “tina”)

11 rep() To repeat values rep(25, times=10)

rep(c(24,”vijay”)),
12 rep( c() , times) To repeat multiple values
times=10)
Y= c(24,15,54,9)
13 Y[] Call values
Y[c(1,4)] = 24 & 9
Call all values except bth
14 Y[-b ] Y[-4] = 24,15,54
element
To add notes or
# call all values of Y
15 # lines that we don’t want to run
except 4th element
as command
matrix(c(a,b,c,d), matrix(c(4,5,8,2), nrow
16 Build matrix
nrow = y, byrow = T) = 2, byrow = T)
Command Application Sample

17 dim() Dimensions of data dim(y)

18 head() 1st 6 elements of data head(x)

19 tail() last 6 elements of data tail(x)

20 names() Headings of data names(x)

21 attach() Attach data in R search path attach(data)

Class i.e. numeric, factor,


22 class() class(x)
integer , etc.

23 level() In factor class, categories level(x)

Summarize data i.e. mean,


24 summary() median, max, min, quartile summary(x)
values
Refine data with conditions
• Xnew = X[-2:-5] :removes elements
from 2nd to 5th
• Xnew = x[x!= 5] : removes all 5 from x
• Teen1 = age[age>= 13 & age<20] :and
• Teen2 = age[result== “pass” | age==“f”] :or
• A= as.integer(result) :changes class
• Aa= cbind(teen1,teen2) :combines data
• Ab= rbind(teen1,teen2)
How to import data
• To import data from
1. .csv data file
– Abc= read.csv(file.choose(), header = T)
2. .txt data file
– Abc = read.delim(file.choose(), header=T,
sep=“\t”)
3. Other file type
– Abc = read.table(file.choose(), header=T, sep=“,”)
How to export data
• To export data to
1. .csv data file
– Abc = write.csv(datasource ,
file=“/users/A/desktop/R/abc2.csv”, row.names = F)
2. .txt data file
– Abc= write.delim(datasource ,
file=“/users/A/desktop/R/abc2.txt”, row.names = F, sep=“\t”)
3. Other file type
– Abc= write.table(datasource ,
file=“/users/A/desktop/R/abc2.csv”, row.names = F, sep=“,”)
Sr. Application Command

1. Create subsets passData = data[result = = “pass”]

2. Median , mean mean(age)

Std. deviation ,
3. sd(age) , var(age)
variance

4. Quartile quantile(age, 0.05)

5. correlation cor(age, height)

6. covariance cov(age,height)

Gives indices for


7. which(x<45, arr.ind = T)
“TRUE” values

8. To concatenate paste(“abc”, 1:2, sep=“_”)

Name rows &


9. colnames, rownames
columns

10. Save workspace save.image


Bar charts
• t : table(result)
Plot bar chart
– barplot(t)
– barplot(t, main=“title”, xlab = “result”, ylab= “n”,
las=1, names.arg=c(“male”, “female”), horiz=F, col=
“RED”, border= “blue”, cex.axis = 0.5, cex.names= 2)
Change characteristics of axis
– axis(1/2 , at= seq(0,15,3), seq(0,30,6))
Add legends
– legend(x=4.5,y=5,legend=c(“male”,”female”),
col=c(4,2), pch= c(15, 4), cex=0.6)
Box & whisker plot
• boxplot(class1,class2)
• Box-whisker plot gives:
– Lower quartile
– Upper quartile
– Median
– Min. value
– Max. value
– Distribution of data
• Agegroup = cut(age, breaks=
c(0,13,30,45,60,80), labels= c("<13", "14-30",
"31-45", "46-60", ">80"))
• points(age[result==“p”], col= “red”, pch=17)

• lines(smooth.spline(age[result=="p"] ),lwd= 3)
• abline(lm(height~age))
• abline(h= 60)

You might also like