Vector and matrix in R language
Hello everyone, In the last post We all saw about R language basic concept which was pretty easy right, so today we are going to start with vectors and matrix in R language. So let's start.
Vector: vector consist of Data element in of same data type. The data element of a vector are called component.
To create a vector we use a function i.e. c().
Example: 1) a <- c(1,2,3,4,5,6,7)
Print (a)
Output: 1,2,3,4,5,6,7.
2) a<-c(1:10)
print (a)
Here colon is used as a range from 1 to 10
Output: 1,2,3,4,5,6,7,8,9,10.
To find a length of vector we use a function length ().
length (a) # output will be 10 for second example.
To print a particular element in vector we use a square bracket ([]) i.e. a[5] # This will print the fifth Element of vector.
Matrix: Matrix is a collection of data element which are arrange in 2dimensional rectangular layout.
To create a Matrix we use a function matrix().
Syntax: matrix(data, nrow,ncol)
- Where data is the input in vector
-nrow stand's for number of rows
Example: # program to create Matrix
a<-matrix(c(1,2,3,4,5,6,7,8), nrow= 2,ncol=4)
print (a)
Output: 1 3 5 7
2 4 6 8
In next post we will see about list and Array.
Comments
Post a Comment