R Vecotors

  • A list of items of the same data type
  • Use the c() to combine a comma seperated list of items into a vector.

Types of Vectors

  • Numeric Vector:
    • Example: c(1.5, 2.3, 3.1, 4.2)
  • Character Vector:
    • Example: c("apple", "banana", "orange")
  • Logical Vector:
    • Example: c(TRUE, FALSE, TRUE, TRUE)
  • Integer Vector:
    • Example: c(1L, 2L, 3L, 4L)
  • Complex Vector:
    • Example: c(1 + 2i, 3 - 4i, 5 + 6i)

Vecotors may also be created using c() or the concatenate function

Find out how many items are in a vector length()

Sort a Vector

syntax: sort(x, decreasing = FALSE, na.last = NA, …)

  • x: The vector or array to be sorted.
  • decreasing: A logical value indicating whether the sorting should be in decreasing order. The default is FALSE for ascending order.
  • na.last: A logical value indicating whether missing values (NA) should be placed at the end. The default is TRUE, meaning missing values are placed at the end.

Access Vectors

Use square brackets ([]) to access specific elements. Example:

Change an Item

Repeat Vectors

The rep() is used to replicate elements of a vector.

Syntax: rep(x, times)

Repeat Elements of a scalar

Repeat Elements of a Vector

seq():Generating Sequenced Vectors

  • This is used to generate sequences of numbers
  • It creates a sequence of numbers from a starting value to an ending value with a specified increment.

Syntax: seq(from, to, by = increment)


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top