Vectors data structures in R consist of order collection of numbers, characters, or strings. There are two major types of vectors:
To create a vector named x
, with values namely 10, 20, 30, 3.5
and 3.8
, use c()
function in R command:
x <- c(10, 20, 30, 3.5, 3.8)
Note: The
c()
function will return a vector.
There are various methods or operators in R to perform assignment operations. Let’s discuss some techniques to perform this operation on vectors.
<-
operatorThis assignment operator(<-
) consists of two characters:
<
(less than)-
(minus)These two characters occur side by side, while the tip points to a variable will receive an assignment value. The assignment of <-
operator is also possible in reverse direction ->
.
Let’s look at the code below:
# USING <- OPERATORcat("Arrow assignment: ")x <- c(10, NA, 30, 'shot', 3.8)cat(x, end='\n')# Assignment operator in reverse directionc(10.4, 5.6, 3.1, 6.4, 'AB') -> ycat("Revered arrow assignment: ", y, end='\n')
c()
function and assign it to the x
variable using <-
operator.x
on console.c()
function and assign it to y
variable using ->
reversed operator.y
to the console.=
operatorEqual to (=
) operator is also used for value assignment. So, we can perform the above operation like:
x = c(9, 8, 23, 4.5, 9.8)
Let’s understand the equal =
operator with the help of a code example:
# USING = OPERATORx = c(10, NA, 30, 'shot', 3.8)cat(x)
x
using the c()
function and equal to =
operator.x
to the console.assign()
Assignment can also be made by using the assign()
method. It’s an equivalent method of assignment as above.
In the example below, we use the assign()
function in detail:
# USING assign() FUNCTIONassign("x", c(10, NA, 30, 'Edpresso', 3.8))cat(x)
assign()
function for assignment second argument value to first one.x
to the console.