Professional Documents
Culture Documents
Why r?
- R is free
- High demand in industry
- Graphing designing quality
- Easy to develop model
More Points
- R is freely available
- Large collection of Packages
- Used in data analysis
- Over 7000 packages available
Variable in R Programming
Examples:
x=11
print (x)
or use
y<-9
ls()
y=?
==
Rm(y)
Y
x.1=10
x.1
xx=”hello world”
xx
yy=’’1”
yy
arithmetic operations:
11+14
25
11-5
55*585
111/20
X=12
Y=12
X+y
X=9
X^2
B1
X^3
X^2+y^2
250
Sqrt(81)
9
X^(1/2)
Log(3)
Log2(3)
Exp(4)
Abx(-14)
14
To add
Vectors in R Programing
Go to Rstudio
Gender =c(“male”,”female”)
Gender
“male” “female”
Examples
11+22
[1] 33
> gender=c("male","female")
> gender
[1] "male" "female"
> data=c(1,2,3,4,5,6,7)
> data
[1] 1 2 3 4 5 6 7
> 2:7
[1] 2 3 4 5 6 7
> seq(from =1,to=7,by=2)
[1] 1 3 5 7
> seq(from=1,to=7,by=1/3)
[1] 1.000000 1.333333 1.666667 2.000000
[5] 2.333333 2.666667 3.000000 3.333333
[9] 3.666667 4.000000 4.333333 4.666667
[13] 5.000000 5.333333 5.666667 6.000000
[17] 6.333333 6.666667 7.000000
>
> seq(from=1,to=7,by=0.2)
[1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6
[10] 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4
[19] 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2
[28] 6.4 6.6 6.8 7.0
rep(23,10)
[1] 23 23 23 23 23 23 23 23 23 23
rep(1:3,times=5)
[1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
rep(seq(from=2,to=9,by=0.25),times=3)
[1] 2.00 2.25 2.50 2.75 3.00 3.25 3.50
[8] 3.75 4.00 4.25 4.50 4.75 5.00 5.25
[15] 5.50 5.75 6.00 6.25 6.50 6.75 7.00
[22] 7.25 7.50 7.75 8.00 8.25 8.50 8.75
[29] 9.00 2.00 2.25 2.50 2.75 3.00 3.25
[36] 3.50 3.75 4.00 4.25 4.50 4.75 5.00
[43] 5.25 5.50 5.75 6.00 6.25 6.50 6.75
[50] 7.00 7.25 7.50 7.75 8.00 8.25 8.50
[57] 8.75 9.00 2.00 2.25 2.50 2.75 3.00
[64] 3.25 3.50 3.75 4.00 4.25 4.50 4.75
[71] 5.00 5.25 5.50 5.75 6.00 6.25 6.50
[78] 6.75 7.00 7.25 7.50 7.75 8.00 8.25
[85] 8.50 8.75 9.00
> rep(c("m","f"),times=6)
[1] "m" "f" "m" "f" "m" "f" "m" "f" "m"
[10] "f" "m" "f
> x=c(1,2,3,4,5,6)
> y=c(3,5,6,7,9,11)
> x
[1] 1 2 3 4 5 6
> y
[1] 3 5 6 7 9 11
>
To add above correspondet values
X+10
> x=c(1,2,3,4,5,6)
> y=c(3,5,6,7,9,11)
> x
[1] 1 2 3 4 5 6
> y
[1] 3 5 6 7 9 11
> x+10
[1] 11 12 13 14 15 16
> x-10
[1] -9 -8 -7 -6 -5 -4
> x*2
[1] 2 4 6 8 10 12
> x/10
[1] 0.1 0.2 0.3 0.4 0.5 0.6
>
To add above correspondence vectors
[1] 3 5 6 7 9 11
> x+10
[1] 11 12 13 14 15 16
> x-10
[1] -9 -8 -7 -6 -5 -4
> x*2
[1] 2 4 6 8 10 12
> x/10
[1] 0.1 0.2 0.3 0.4 0.5 0.6
> x+y
[1] 4 7 9 11 14 17
> x-y
[1] -2 -3 -3 -3 -4 -5
> x*y
[1] 3 10 18 28 45 66
> x/y
[1] 0.3333333 0.4000000 0.5000000 0.5714286
[5] 0.5555556 0.5454545
>
To find particular vectors
> y[3]
[1] 6
> y[-3]
[1] 3 5 7 9 11
> y[1:3]
[1] 3 5 6
> y[-c(1,5)]
[1] 5 6 7 11
> y[y<8]
[1] 3 5 6 7
> y[y>8]
[1] 9 11
Matrix in R Programing
> matrix(c(1,2,3,4,5,6,7,8,9),nrow = 3,byrow = TRUE)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> matrix (C(1,2,3,4,5,6,7,8,9,),nrow = 3,byrow = FALSE)
Error in C(1, 2, 3, 4, 5, 6, 7, 8, 9, ) :
object not interpretable as a factor
> matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,byrow=FALSE)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
>
To find first row’s second elements
Then
>
Using table command
>
How to load text file from excel to R Programing
Than
> data3=read.delim(file.choose(),header=T)
> data3
name class age
1 a 1 12
2 b 2 23
3 c 3 24
> data4=read.table(file.choose(),header = T,sep="\t")
> data4
name class age
1 a 1 12
2 b 2 23
3 c 3 24
Path Method to load file into R Programing
> data5=read.table(file="C:/Users/Kamran/Desktop/tet.txt",header=T,sep="\t")
> data5
name class age
1 a 1 12
2 b 2 23
3 c 3 24
To know how many row and coloum
dim(data1)
[1] 3 3
> dim(data3)
[1] 3 3
> head(data1)
NAME CLASS AGE
1 A 1 10
2 B 2 11
3 C 3 12
> tail(data1)
NAME CLASS AGE
1 A 1 10
2 B 2 11
3 C 3 12
> data4[c(2,3),]
name class age
2 b 2 23
3 c 3 24
> data4[c(2,3),1]
[1] b c
Levels: a b c
> names(data1)
[1] "NAME" "CLASS" "AGE"
>