# R Vector: How to Create, Access, and Modify Vector Elements

Vector is the easiest data type in R programming language. Vector can contain multiple elements, but all the elements are of the same data type. Vector has a property called length, which returns the number of elements in the vector.

In R, you can check the data type of any variable using the typeof() method.

## R Vector

R Vector is a sequence of data items of the same data type. Elements in a vector are officially called components. It can contain an integer, double, character, logical,  complex, or raw data types.

## How to Create a Vector in R?

To create a Vector in R, we generally use the c() function, but the c() function stands for concatenate. It doesn’t create vectors; it just combines them.

The vector elements must have the same data type, and if they are different, then the function will try and coerce elements to the same type. Coercion here means from lower to higher types from logical to an integer to double to the character.

Logical vector elements are initialized to FALSE, numeric vector elements to 0, character vector elements to “”, raw vector elements to nul bytes, and list/expression elements to NULL.

``````# Pro.R

rv <- c(11, 21, 19)
cat(rv, "\n")
cat(typeof(rv))``````

#### Output

``````Rscript Pro.R
11 21 19
double``````

In this example, we have created a vector using double values.

### Vector length()

To check the length of a Vector in R, use the length() function.

``````rv <- c(11, 21, 19)
cat(length(rv))``````

#### Output

``3``

It counts the number of elements in the Vector.

### Creating a vector using: operator

We can create a Vector using a colon(:) operator.

``````rv <- 1:10
cat(rv)``````

#### Output

``1 2 3 4 5 6 7 8 9 10``

### Create a vector using seq() function

We can create a Vector using the seq() function in R. To generate a standard generic sequence in R, use the seq() method.

``````rv <- seq(1, 5, by = 0.8)
cat(rv)``````

#### Output

``1 1.8 2.6 3.4 4.2 5``

### Vector of Logical Values

Create a Vector of logical values. In R, TRUE and FALSE are logical values. 1 is not TRUE, and 0 is not FALSE in R.

``````rv <- c(TRUE, FALSE, FALSE, TRUE, TRUE)
cat(rv, "\n")
cat(typeof(rv))``````

#### Output

``````TRUE FALSE FALSE TRUE TRUE
logical``````

### Create a Vector of Character strings

Let’s create a vector of logical values.

``````rv <- c("KRU", "NAL", "halloween", "Petrnous")
cat(rv, "\n")
cat(typeof(rv))``````

#### Output

``````KRU NAL halloween Petrnous
character``````

## How to Access Elements of a Vector in R

To access elements in R Vector, use vector indexing. You can access the values of a vector by declaring an index inside a single square bracket “[ ]” operator.

The indexing vector can be categorized in the following.

1. Logical vector
2. Integer vector
3. Character vector

Unlike other programming languages, the square bracket operator returns more than just individual members.

### Using a logical vector as an index

If we use a logical vector for indexing, the position where the logical vector is TRUE is returned.

``````rv <- c(11, 19, 21, 46, 18)
cat(rv[c(TRUE, TRUE, TRUE, FALSE, FALSE)])``````

In this example, the first three indexes are TRUE, TRUE, and TRUE. That means it will return the first three elements in the output; if it encounters FALSE, it won’t return the relevant vector element.

``11 19 21``

### Using integer vector as an index

In R programming, the vector index starts from 1. If you are familiar with other languages, then the index usually starts at 0.

We can use the vector of integers as an index to access particular items. If we use the negative vector of integers, then it will return all elements except that those specified.

``````rv <- c(11, 19, 21, 46, 18)
cat(rv)``````

#### Output

``21``

Let’s access two elements. To access multiple elements, pass the vector of the index.

``````rv <- c(11, 19, 21, 46, 18)
cat(rv[c(2, 3, 4)])``````

#### Output

``19 21 46``

If the index is negative, it will remove the element whose position has the same absolute value as the negative index. Pass the negative vector of integers.

``````rv <- c(11, 19, 21, 46, 18)
cat(rv[c(-2, -3, -4)])``````

#### Output

``11 18``

### Using character vector as an index

When dealing with named vectors, then using a character vector as an index is all you need.

``````rv <- c("one" = 21, "two" = 11, "three" = 19)
cat(rv["three"])``````

#### Output

``19``

## How to modify a vector in R

To modify a vector in R, use the assignment operator(<-). Modifying the vector is a straightforward task. Let’s see how to do it.

``````rv <- c(11, 18, 19, 23, 46)
cat("Before modifying the vector \n")
cat(rv, "\n")
rv <- 21
cat("After modifying the vector \n")
cat(rv)``````

#### Output

``````Before modifying the vector
11 18 19 23 46
After modifying the vector
11 18 19 21 46``````

You can see that we have modified the 4th element in the vector using the assignment operator.

To truncate the element of the vector, use the color operator.

``````rv <- c(11, 18, 19, 23, 46)
cat("Before truncating the vector \n")
cat(rv, "\n")
rv <- rv[1:3]
cat("After truncating the vector \n")
cat(rv)``````

#### Output

``````Before truncating the vector
11 18 19 23 46
After truncating the vector
11 18 19``````

From the output, you can see that it has truncated the last two elements and only included the first three elements.

## How to delete a Vector in R

To delete a Vector in R, assign NULL to the vector.

``````rv <- c(11, 18, 19, 23, 46)
cat(rv, "\n")
cat("After deleting the Vector\n")
rv <- NULL
print(rv)``````

#### Output

``````11 18 19 23 46
After deleting the Vector
NULL``````

You can see that the vector rv is now NULL. That means it is now deleted.

## How to sort Vector in R

To sort a vector in R, use the inbuilt sort() method. By default, it sorts in ascending order.

``````rv <- c(11, 18, 19, 10, 46)
res <- sort(rv)
cat(res)``````

#### Output

``10 11 18 19 46``

To sort in descending order, we can pass decreasing=TRUE.

``````rv <- c(11, 18, 19, 10, 46)
res <- sort(rv, decreasing = TRUE)
cat(res)``````

#### Output

``46 19 18 11 10``

## Conclusion

Vectors in R are the data structures containing numeric, integer, complex, character, or logical data type. It is somewhat similar to an array in other languages, but it starts at 1 instead of 0. You can add, modify, and delete the vectors.