Documentos de Académico
Documentos de Profesional
Documentos de Cultura
> 1+1
[1] 2
> 1-1
[1] 0
> variable1=1+1
> variable1
[1] 2
> variable1
[1] 2
> 1 + 2 / 3 - 2 * 6.5
[1] -11.33333
> 1 * (2 / (1 + 1))
[1] 1
>1*2/1+1
[1] 3
#Vector is a set of number either is a single row or column, with one direction
> vecter1
> vector1
[1] 1 2 3 4 5 6 7 8 9
> 1.2:9
> vector2
> vector3
[1] 1 2 3 4 5 6 7 8 9
> vector4=9:1
> vector4
[1] 9 8 7 6 5 4 3 2 1
> vector3+vector4
[1] 10 10 10 10 10 10 10 10 10
> vector5=1:3
> vector3
[1] 1 2 3 4 5 6 7 8 9
> vector3+vector5
[1] 2 4 6 5 7 9 8 10 12
#R can be used to represent matrix, matrix calculation and even picking particular
element from a matrix.
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> matrix(vector3, nrow = 2)
[1,] 1 3 5 7 9
[2,] 2 4 6 8 1
Warning message:
In matrix(vector3, nrow = 2) :
data length [9] is not a sub-multiple or multiple of the number of rows [2]
> 1+1
[1] 2
> vector3
[1] 1 2 3 4 5 6 7 8 9
[1,] 1 2 3 4 5 6 7 8 9
> matrix1 + 2
[1,] 3 6 9
[2,] 4 7 10
[3,] 5 8 11
[1,] 2 8 14
[2,] 4 10 16
[3,] 6 12 18
> matrix1%*%matrix1
[,1] [,2] [,3]
[1,] 30 66 102
[2,] 36 81 126
[3,] 42 96 150
[1,] 1 16 49
[2,] 4 25 64
[3,] 9 36 81
> t(matrix1)
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> matrix1
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> matrix1[1, 3]
[1] 7
> matrix1[ 2, ]
[1] 2 5 8
> matrix1[,-2]
[,1] [,2]
[1,] 1 7
[2,] 2 8
[3,] 3 9
> matrix1[1, 1] = 15
> matrix1
[1,] 15 4 7
[2,] 2 5 8
[3,] 3 6 9
> matrix1[ ,2 ] = 1
> matrix1
[1,] 15 1 7
[2,] 2 1 8
[3,] 3 1 9
> matrix1
[1,] 15 2 2
[2,] 2 2 2
[3,] 3 2 2
> matrix1
[1,] 15 4 7
[2,] 2 5 8
[3,] 3 6 9
> matrix1
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[1] 6 7 8 9
> matrix1
[1,] 1 4 7
[2,] 2 5 3
[3,] 3 6 3
#for loop in R
[1] 1
[1] 6
[1] 2
[1] 7
[1] 3
[1] 8
> matrix3=matrix1+5
> print(matrix3)
[1,] 6 9 12
[2,] 7 10 8
[3,] 8 11 8
> print(matrix3)
[1,] 1 4 7
[2,] 2 5 3
[3,] 3 6 3
> print(matrix1)
[1,] 1 4 7
[2,] 2 5 3
[3,] 3 6 3
#for loop to replace matrix element with 0 first row and first column, second row
and first column and third row and first column with zero where if element value is
greater than or equal to 2.
[1,] 1 4 7
[2,] 0 5 3
[3,] 0 6 3
> vector6
[1] 9 8 7
#While loop which checks if sum of vector6 is greater than or equal to 6 then it will
execute and print value of vector6 by subtracting 1 from each value till sum of
vector6 < 6.
+ vector6=vector6-1
+ print(vector6)
+}
[1] 8 7 6
[1] 7 6 5
[1] 6 5 4
[1] 5 4 3
[1] 4 3 2
[1] 3 2 1
[1] 2 1 0
[1,] 0 0 1
[2,] 0 0 0
[3,] 0 1 0
>