R Language: Creating Vectors

0
The `c()` function can be used to create vectors of objects by concatenating things together. The `vector()` function can also be used to initialize vectors.

Here are some examples of how to create vectors using the `c()` function:

x <- c(0.5, 0.6)       ## numeric
x <- c(TRUE, FALSE)    ## logical
x <- c(T, F)           ## logical
x <- c("a", "b", "c")  ## character
x <- 9:29              ## integer
x <- c(1+0i, 2+4i)     ## complex

As you can see, the `c()` function can be used to create vectors of any type of object. The objects in the vector can be of different types, but they will all be coerced to the same type. For example, if you create a vector with a mix of numeric and logical objects, the logical objects will be coerced to numeric.

The `vector()` function can also be used to initialize vectors. The `vector()` function takes two arguments: the class of the vector and the length of the vector. For example, the following code creates a vector of 10 numeric objects:

x <- vector("numeric", length = 10)

The `vector()` function will initialize the vector with all zeros. You can also initialize the vector with other values using the `vector()` function. For example, the following code creates a vector of 10 character objects, each initialized to the value "hello":

x <- vector("character", length = 10, init = "hello")

Post a Comment

0Comments
Post a Comment (0)