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
c() function in R command:
x <- c(10, 20, 30, 3.5, 3.8)
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.
This assignment operator(
<-) consists of two characters:
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 <- OPERATOR cat("Arrow assignment: ") x <- c(10, NA, 30, 'shot', 3.8) cat(x, end='\n') # Assignment operator in reverse direction c(10.4, 5.6, 3.1, 6.4, 'AB') -> y cat("Revered arrow assignment: ", y, end='\n')
c()function and assign it to the
c()function and assign it to
yto the console.
Equal 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 = OPERATOR x = c(10, NA, 30, 'shot', 3.8) cat(x)
c()function and equal to
xto the console.
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() FUNCTION assign("x", c(10, NA, 30, 'Edpresso', 3.8)) cat(x)
assign()function for assignment second argument value to first one.
xto the console.
View all Courses