types of vectors in r

Types of Vectors in R

In R, there are several types of vectors that can be used to store and manipulate data. These include:

  1. Numeric Vectors: Numeric vectors are used to store numeric values such as integers or decimal numbers. They are created using the c() function, which combines individual values into a vector. For example, x <- c(1, 2, 3, 4) creates a numeric vector x with values 1, 2, 3, and 4.

  2. Character Vectors: Character vectors are used to store text or string values. They are created by enclosing the text within quotes. For example, x <- c("apple", "banana", "orange") creates a character vector x with the values "apple", "banana", and "orange".

  3. Logical Vectors: Logical vectors are used to store logical values, which can be either TRUE or FALSE. They are often used for logical operations and comparisons. For example, x <- c(TRUE, FALSE, TRUE) creates a logical vector x with the values TRUE, FALSE, and TRUE.

  4. Integer Vectors: Integer vectors are a specialized type of numeric vector that can only store whole numbers. They are created by appending an L to the numeric values. For example, x <- c(1L, 2L, 3L) creates an integer vector x with the values 1, 2, and 3.

  5. Complex Vectors: Complex vectors are used to store complex numbers, which consist of a real part and an imaginary part. They are created using the complex() function. For example, x <- complex(real = c(1, 2), imaginary = c(3, 4)) creates a complex vector x with the values 1+3i and 2+4i.

  6. Raw Vectors: Raw vectors are used to store raw bytes of data. They are created using the charToRaw() function. For example, x <- charToRaw("Hello") creates a raw vector x with the values 48, 65, 6C, 6C, and 6F.

These are the main types of vectors in R, each with its own purpose and usage. By understanding these vector types, you can effectively store and manipulate different types of data in R.