Listado de la etiqueta: Vector


In this R tutorial, we will do all the following operations that are performed on a vector:

  1. Create
  2. Access
  3. Modify
  4. Delete
  5. Sort

We know that a vector is a flat data structure used to store data in a linear fashion.

Create

Vectors can be created using c(). The “c” stands for combine.

Syntax:

vector_object=c(values…)

The values are the elements that are separated by a comma.

Example 1

In this example, we will create a vector with five elements and print them.

#create vector for fruits
fruits=c(23,4,43,3,4)

print(fruits)

Result:

It is also possible to create a vector using the sequence operator -:

We need to specify the start and end with this.

Example 2

In this example, we will create a vector with five elements and print them.

#create vector for fruits from 100 to 104
fruits=c(100:104)

print(fruits)

Result:

Access

We can access the values in the vector using index positions.

To access the single element, we can directly specify the index position.

In the R language, for any data structure, the indexing starts with 1.

Syntax:

vector_object[index]

Where vector_object is the vector and index that specify index position.

Example

In this example, we will return the elements based on index position.

#create vector for fruits from 100 to 104
fruits=c(100:104)

#get second element from fruits
print(paste(«Second element: «,fruits[2]))

#get fifth element from fruits
print(paste(«Fifth element: «,fruits[5]))

#get first element from fruits
print(paste(«First element: «,fruits[1]))

Result:

We returned the elements present at the second, fifth, and first positions.

To access the multiple elements, we can directly specify the index position inside c(). This returns elements with respect to the index positions.

Syntax:

vector_object[c(index,….)]

Where vector_object is the vector and indices specify the index positions.

Example

In this example, we will return the elements based on index positions at a time.

#create vector for fruits from 100 to 104
fruits=c(100:104)

#get elements from second, fifth, and 1st positions.
print(fruits[c(2,5,1)])

Result:

We returned the elements present at the second, fifth, and first positions at a time.

Modify

We can modify the values in the vector using index positions.

To access the single element, we can directly specify the index position.

In the R language, for any data structure the indexing starts with 1. So, we can assign the value at a particular index position.

Syntax:

vector_object[index]=updated_element

Where vector_object is the vector and index specify the index position to set the updated element.

Example

In this example, we will update elements at different index positions.

#create vector for fruits from 100 to 104
fruits=c(100:104)

#display coetáneo fruits
print(fruits)

#update the value to 45 at index-2
fruits[2]=45

#update the value to 15 at index-5
fruits[5]=15

#update the value to 12 at index-1
fruits[1]=12

#display updated fruits
print(fruits)

Result:

We updated 100 with 12 in the first position, 101 with 45 in the second position, and 104 to 15 in the fifth position.

To update the element at multiple positions, we can directly specify the index positions inside c().

Syntax:

vector_object[c(index,….)]=updated_value

Where vector_object is the vector and indices specify the index positions.

Example

In this example, we will update values at different index positions at a time with an element.

#create vector for fruits from 100 to 104
fruits=c(100:104)

#display coetáneo fruits
print(fruits)

#update the value to 45 at indices 2,5,1
fruits[c(2,5,1)]=45

#display updated fruits
print(fruits)

Result:

We updated the first, second, and fifth positions with 45.

Delete

It is possible to delete an entire vector by assigning it to NULL.

Syntax:

vector_object =NULL

Where vector_object is the vector.

Example

In this example, we will delete the fruits vector.

#create vector for fruits from 100 to 104
fruits=c(100:104)

#display coetáneo fruits
print(fruits)

#specify NULL
fruits=NULL

#display updated fruits
print(fruits)

Result:

We can see that the vector is no more.

Sort

It is possible to sort the vector in ascending or descending order using the sort() function.

It takes two parameters. First is the vector object, and second is the correlating that takes the Boolean values.

Syntax:

sort(vector_object,decreasing=TRUE?FALSE)

Parameters:

  1. vector_object is the vector.
  2. If decreasing is TRUE, then the vector is sorted in descending order. If decreasing is FALSE, then the vector is sorted in ascending order.

Example 1

Sort the fruits vector in ascending order.

#create vector for fruits
fruits=c(45,32,67,57,54)

#display coetáneo fruits
print(fruits)

#sorted in ascending order
print(sort(fruits,decreasing=FALSE))

Result:

We can see that elements are sorted in ascending order.

Example 2

Sort the fruits vector in descending order.

#create vector for fruits
fruits=c(45,32,67,57,54)

#display coetáneo fruits
print(fruits)

#sorted in descending order
print(sort(fruits,decreasing=TRUE))

Result:

We can see that the elements are sorted in descending order.

Conclusion

In this R tutorial, we saw how to create a vector using c(), while accessing the elements from a vector through the index positions. We can modify and update the vector by setting the updated element to the index position. NULL is assigned to a vector if we want to delete a vector. Finally, we have seen how to sort a vector object in ascending and descending order.



Source link