We can create Vector in R using "C" or "Concatenate Command"
Ex:
x1 = c(1,2,3,4,5)
print (x1) or x1
-----------------------------------
We can also create a vector of character elements by including quotations.
Ex:
x1= c("male","female")
print(x1) or x1
-----------------------------------
Integer values using colon (:) Creating sequence from value to TO value.
Ex:
2:7
2,3,4,5,6,7
---------------------------------
Incremental by:
Sequence from 1 to 7 increment of 1
Syntax : seq(from=1, to =7, by=1)
1,2,3,4,5,6,7 as output.
we also can use like below
seq(from=1, to =7, by=1/3)
seq(from=1, to =7, by=0.25)
-------------------------------------------
Repeated Characters:
rep(x, times=y)
Ex:
rep(1 , times=10)
1,1,1,1,1,1,1,1,1,1
rep("divakar", times=5)
divakar,divakar,divakar,divakar,divakar
rep(1:3, times=3)
1,2,3,1,2,3,1,2,3
rep(seq(from=2,to=5,by=0.25,
Other Ex:
x<- 1:5
1,2,3,4,5
y <-c(3,4,5,6)
3,4,5,6
x +10 = 11,12,13,14,15
x -10
x*10
y/2
Extract Positions:
x = 1,2,3,4
x[3] = 4 ( positions starts with 0,1,2,3,4)
x[-2] = 1,2,4 ( extract all the elements expect 2 position element.
--------------------------------------
If both Vectors are having same length we can add/subtract/Multiply them.
Ex:
x = 1,2
y=3,4
x+y = 4,6
---------------------------
Matrix:
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
byrow = TRUE elements will be entered in row-wise fashion
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
byrow = FALSE elements will be entered in Column-Wise fashion
------------------------------------
Assigning matrix:
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
mat <- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,byrow=FALSE)
Extracting 1 row and 2nd Column.
mat [1,2]
[1] 4
Extract 1, 3 row and 2 Column
mat [c(1,3),2]
[1] 4 6
mat(2,) Extract all the columns from row 2.
mat(,1) Extract all the rows from Column 1
mat*10 multiply all elements with 10
-------------------------------------------
No comments:
Post a Comment