0% found this document useful (0 votes)
10 views9 pages

Ps 1

Uploaded by

azziesksk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Ps 1

Uploaded by

azziesksk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Basic functions of R

Aim- To perform some basic functions of R


CODE
> x<-10
#assigns the value of 10 to x
>x
#prints the value of x
[1] 10
> y=x*2
#assigns the value of x*2 to y
>y
#print the value of y
[1] 20
> z=x+y
#z is the sum of x and y
>z
#print the value of z
[1] 30
> X=25
#capital x is considered another variable
> x+X
#print the sum of x+X
[1] 35
> x=1,2,3,4,5
Error: unexpected ‘,’ in “x=1,”
> x=c(1,2,3,4,5)
#use of =c() function to create vector
>x
#print the value of x
[1] 1 2 3 4 5
> 2+5
#R as a calculator
[1] 7
> 2*5
#R as a calculator
[1] 10
> 2/4
#R as a calculator
[1] 0.5
> 2*3-4+5/6
#R as a calculator
[1] 2.833333> 2^3
#R as a calculator
[1] 8
> 2**3
#R as a calculator
[1] 8
> c(2,3,4,5)^2
#command application to a vector
[1] 4 9 16 25
> c(2,3,4,5)^c(2,3)
#command application to a vector
[1] 4 27 16 125
> c(2,3,4,5) + 10
#command application to a vector
[1] 12 13 14 15
> c(2,3,5,7) %/% 2
#command application to a vector (gives quotient)
[1] 1 1 2 3
> c(2,3,5,7) %% 2
#modulo operation (gives remainder)
[1] 0 1 1 1
> c(2,3,5,7)/2 # divides
[1] 1 1.5 2.5 3.5
> max(1,4,8,112)
#max function
[1] 112
> max(1.2, 3.2, -7.8)
#max function
[1] 3.2
> min(1,4,8,112)
#min function
[1] 1
> min(1.2, 3.2, -7.8)
#min function
[1] -7.8
> abs(-4)
#absolute value function
[1] 4
> abs(c(-1,-2,-3,4,5))
#absolute value function
[1] 1 2 3 4 5
> sqrt(4)
#square root function
[1] 2
> sqrt(c(4,9,16,25))
#square root function
[1] 2 3 4 5> sum(c(2,3,5,7))
#sum function
[1] 17
> prod(c(2,3,5,7))
#product function
[1] 210
> round(1.83)
#round function
[1] 2
> round(1.23)
#round function
[1] 1
> log(10)
#log function
[1] 2.302585
> log10(10)
#log to the base 10
[1] 1
> log(9,base=3)
#log to the base 3
[1] 2
> var<-3+5i
#variable with a complex value
> var
#print the value of var
[1] 3+5i
> Re(var)
#real part
[1] 3
> Im(var)
#imaginary part
[1] 5
> Conj(var)
#conjugate value
[1] 3-5i
> Mod(var)
#complex modulus
[1] 5.830952
> Arg(var)
#complex argument
[1] 1.030377
> x<-matrix(nrow=4, ncol=2, data=c(1,2,3,4,5,6,7,8))
#matrix assigned to x
>x
#print the matrix x
[,1]
[,2]
[1,] 1
5
[2,] 2
6
[3,] 3
7[4,] 4 8
> x[3,2]
#print the element positioned at [3,2]
[1] 7
> x<-matrix(nrow=4, ncol=2, data=c(1,2,3,4,5,6,7,8), byrow=TRUE) #entering data row-wise
>x
#print the matrix x
[,1]
[,2]
[1,] 1
2
[2,] 3
4
[3,] 5
6
[4,] 7
8
> dim(x)
#dimensions of the matrix
[1] 4 2
> nrow(x)
#number of rows
[1] 4
> ncol(x)
#number of columns
[1] 2
> mode(x)
#Type of storage
[1] “numeric”
> attributes(x)
#dimensions of the matrix
$dim
[1] 4 2
> x<-matrix(nrow=4, ncol=2, data=2)
#assigning specified number to all matrix elements
>x
#print the matrix x
[,1]
[,2]
[1,] 2
2
[2,] 2
2
[3,] 2 2
[4,] 2 2
> x<-diag(1, nrow=2, ncol=2)
#constructing a diagonal matrix
>x
#printing the matrix x
[,1]
[,2]
[1,] 1
0[2,] 0
1
> x<-matrix(nrow=4, ncol=2, data=c(1,2,3,4,5,6,7,8))
#matrix x
>x
#printing the matrix x
[,1]
[,2]
[1,] 1
5
[2,] 2
6
[3,] 3
7
[4,] 4
8
> y<-t(x)
#transpose of the matrix x is y
>y
#print the matrix y
[,1] [,2]
[,3] [,4]
[1,] 1
2
3
4
[2,] 5 6
7
8
> 4*x
#multiplying the matrix with a constant
[,1]
[,2]
[1,] 4
20
[2,] 8
24
[3,] 12
28
[4,] 16
32
> xt<-t(x)%*%x
#matrix multiplication
> xt
#printing the result
[,1]
[,2]
[1,] 30
70
[2,] 70
174
> xt <- crossprod(x)
#cross product of a matrix
> xt
[,1]
[,2]
[1,] 30
70
[2,] 70
174
> x + 6*x
#operations on matrix[,1]
[,2]
[1,] 7
35
[2,] 14
42
[3,] 21
49
[4,] 28
56
> 5*x – x
#operations on matrix’
[,1]
[,2]
[1,] 4
20
[2,] 8
24
[3,] 12
28
[4,] 16
32
> x[2,]
#accessing elements
[1] 2
6
> x[,2]
#accessing elements
[1] 5
6
7
8
> x[,3]
#accessing elements
Error in x[, 3] : subscript out of bounds
> x[3:4, 1:2]
#accessing elements
[,1]
[,2]
[1,] 3
7
[2,] 4
8
> y<-matrix(nrow=2, ncol=2, byrow=T, data=c(83,100,100,110))
#matrix x
>y
#printing the matrix y
[,1]
[,2]
[1,] 83
100
[2,] 100
110
> solve(y)
#inverse of the matrix y
[,1]
[,2]
[1,] -0.1264368 0.1149425
[2,] 0.1149425 -0.0954023
> solve(5,10)
#solving for x[1] 2
> a<-matrix(c(3,1,2,1),nrow=2,ncol=2) #solving for 3x+2y=8 and x+y=2
> b<-matrix(c(8,2),nrow=2,ncol=1)
> solve(a,b)
[,1]
[1,] 4
[2,] -2
>y
[,1]
[,2]
[1,] 83
100
[2,] 100 110
> eigen(y)
#eigen value of matrix y
eigen() decomposition
$values
[1] 197.407136 -4.407136
$vectors
[,1]
[,2]
[1,] 0.6581085 -0.7529231
[2,] 0.7529231 0.6581085
> mymatrix<=matrix(1:6,2,3,byrow=T)
Error: object ‘mymatrix’ not found
> mymatrix<-matrix(1:6,2,3,byrow=T)
#matrix “mymatrix“
> mymatrix
#printing mymatrix
[,1]
[,2] [,3]
[1,] 1
2
3
[2,] 4
5
6
> mymatrix<-rbind(mymatrix, 7:9)
#changing the values of matrix
> mymatrix
[,1]
[,2]
[,3]
[1,] 1
2
3
[2,] 4
5
6
[3,] 7
8
9> mymatrix <- cbind(mymatrix, 6:8)
#changing the values of matrix
> mymatrix
[,1]
[,2] [,3] [,4]
[1,] 1
2
3
6
[2,] 4
5
6
7
[3,] 7
8
9
8
> mymatrix[3,]<-1:4
#changing the entire row
> mymatrix
[,1]
[,2] [,3]
[,4]
[1,] 1
2
3
6
[2,] 4
5
6
7
[3,] 1
2
3
4
> mymatrix[,4]<-7:9
#changing the entire column
> mymatrix
[,1]
[,2] [,3] [,4]
[1,] 1
2
3
7
[2,] 4
5
6
8
[3,] 1
2
3
9
> mymatrix<-mymatrix[-2,]
#deleting one row
> mymatrix
[,1]
[,2] [,3] [,4]
[1,] 1
2
3
7
[2,] 1
2
3
9
> mymatrix<-mymatrix[,-4]
#deleting one column
> mymatrix
[,1]
[,2] [,3]
[1,] 1
2
3
[2,] 1
2
3
> x<-“Nishtha”
#printing name
>x
[1] “Nishtha”

You might also like