- Create vectors using C function :
C Function Combines Values into a Vector or List, This is a generic function which combines its arguments. The default method combines its arguments to form a vector. All arguments are coerced to a common type which is the type of the returned value.
In a Layman Language Generally we use ‘c’ Function to combine different elements together.
Let’s do a quick example : vector creation using C function
Here from the above example, C Function is combining all the Numeric Values into a vector
typeof(x) : This specifies the type of the data type variable x is holding, in the console pane we can see the the type of data type printed is double. As we are aware that R Language by default stores the data of type Double, All the operations, calculations in R are conducted in Doubles because R Language anticipates all the arithmetic operations should be done with Double data type ,so it is Mandatory to mention the L for integer Variable assignments Like this : ( x <- c(1L,10L,5L,9L,2L) )
length(x) : This gives the Length of the vector , in our example, there are 5 Elements, so the Length of the Vector is 5
2. vectors Using Colon (:) operator :
Colon (":"
) is an operator in R that generates regular sequences.
We can generate a vector of integers from 1 to 10 in increasing order or from 10 to 1 in decreasing Order
3. Vector Creation Using sequence Function (seq) :
seq() function in R Language is used to create a sequence of elements in a Vector. It takes the length and difference between values as optional argument.
From This example : Variable x is assigned to Vector of elements from 1 to 5 in a sequence by Incrementing 0.5 to the each elements. The length of Vector is 9
4. Create Vector & Sort it using sort Function :
sort() function in R is used to sort a vector. By default, it sorts a vector in increasing order. To sort in descending order, add a “decreasing” parameter to the sort function.
Here from this example : Variable X contains (9,4,2,7,5,5,3,1,8) and when sorted the x variable and stored the sorted values in Y , y contains (1,2,3,4,5,6,7,8,9) -> sorted in Increasing order
Now Lets Look at Decreasing order :
Here from this example : Variable X contains (1,2,3,4,5,6,7,8,9) and when sorted the x variable and stored the sorted values in Y , y contains (9,8,7,6,5,4,3,2,1) -> sorted in Decreasing order
Let’s explore more on Vector Functions & operations in Next Post…. Happy Coding … 👍