0.1 1-10

0.1.1 Create vectors of zeros of size 10

rep(0,10)
##  [1] 0 0 0 0 0 0 0 0 0 0

0.1.2 Create a vector with values ranging from 10 to 49

1:49
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49

0.1.3 Reverse a vector

v<- c(1,10,20,30)
rev(v)
## [1] 30 20 10  1

0.1.4 Create a 4x3 matrix with values ranging from 0 to 11

v<- 0:11
#matrix(v,4,3) or
matrix(data=v, nrow = 4, ncol=3)
##      [,1] [,2] [,3]
## [1,]    0    4    8
## [2,]    1    5    9
## [3,]    2    6   10
## [4,]    3    7   11
# in python numpy, the matrix is filled row-wise first unlike in R where column is filled first. 
# we can make the same by transposing
t(matrix(v,3,4))
##      [,1] [,2] [,3]
## [1,]    0    1    2
## [2,]    3    4    5
## [3,]    6    7    8
## [4,]    9   10   11

0.1.5 Find indices of non-zero elements from [1,2,0,0,4,0]

v<-c(1,2,0,0,4,0)
which(!v==0)
## [1] 1 2 5

0.1.6 Create a 3x3 identity matrix

diag(3)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

0.1.7 Create a 3x3x3 array with random values

array(runif(9),dim=c(3,3,3)) # create six random numbers as vector, then arrange as matrix
## , , 1
## 
##            [,1]      [,2]      [,3]
## [1,] 0.00939768 0.3088938 0.2595010
## [2,] 0.53535069 0.3616628 0.6331503
## [3,] 0.65492216 0.6957565 0.7657420
## 
## , , 2
## 
##            [,1]      [,2]      [,3]
## [1,] 0.00939768 0.3088938 0.2595010
## [2,] 0.53535069 0.3616628 0.6331503
## [3,] 0.65492216 0.6957565 0.7657420
## 
## , , 3
## 
##            [,1]      [,2]      [,3]
## [1,] 0.00939768 0.3088938 0.2595010
## [2,] 0.53535069 0.3616628 0.6331503
## [3,] 0.65492216 0.6957565 0.7657420

0.1.8 Create a 10x10 array with random values and find the minimum and maximum values

a<-matrix(runif(100),10,10)
max(a);min(a)
## [1] 0.9821908
## [1] 0.004889003

0.1.9 Create a random vector of size 30 and find the mean value

mean(runif(30))
## [1] 0.5209409

0.1.10 Create a 2d array with 1 on the border and 0 inside

z<-matrix(rep(1,100),10,10)
z[2:9,2:9] <- 0
z
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]    1    1    1    1    1    1    1    1    1     1
##  [2,]    1    0    0    0    0    0    0    0    0     1
##  [3,]    1    0    0    0    0    0    0    0    0     1
##  [4,]    1    0    0    0    0    0    0    0    0     1
##  [5,]    1    0    0    0    0    0    0    0    0     1
##  [6,]    1    0    0    0    0    0    0    0    0     1
##  [7,]    1    0    0    0    0    0    0    0    0     1
##  [8,]    1    0    0    0    0    0    0    0    0     1
##  [9,]    1    0    0    0    0    0    0    0    0     1
## [10,]    1    1    1    1    1    1    1    1    1     1

0.2 to be continued

0.2.0.1 file details

file:"C:\Users\mahes\100 exercises in R.Rmd"