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:
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 vectorx
with values 1, 2, 3, and 4.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 vectorx
with the values "apple", "banana", and "orange".Logical Vectors: Logical vectors are used to store logical values, which can be either
TRUE
orFALSE
. They are often used for logical operations and comparisons. For example,x <- c(TRUE, FALSE, TRUE)
creates a logical vectorx
with the valuesTRUE
,FALSE
, andTRUE
.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 vectorx
with the values 1, 2, and 3.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 vectorx
with the values 1+3i and 2+4i.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 vectorx
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.