Site icon Spark By {Examples}

How to Create a Vector in R and Access it?

r create vector

You can create a Vector in R using c() primitive function. In R programming, the Vector contains elements of the same type and the types can be logical, integer, double, character, complex or raw. Besides c() you can also create a vector using vector(), character() functions.

The following ways of creating vectors in R are covered in this article.

1. Create a Vector in R using c() Function

You can create the Vector in R with one or more elements by using the c() function (which stands for “combine” or “concatenate”). A Vector is a fundamental data structure that is used to store elements of the same data type. and the types can be logical, integer, double, character, complex, or raw. Let’s see the syntax of this function and how to create a vector.

1.1. Syntax of c()

Following is a syntax of the c() function that is used to create a Vector in R.


# Syntax of c() function
c(...)

1.2. Create a Vector Example

Using c() function is the most used and common way to create a vector in R. Actually c() is a combined function that is used to combine elements into a vector or list. The following example creates a Numeric Vector, Character Vector, and Date Vector with variable names id, name, and dob respectively.


# Create Vectors
id <- c(10,11,12,13)
name <- c('sai','ram','deepika','sahithi')
dob <- as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16'))

Here variables

The above example creates 3 Vectors, now let’s display the type of these vector variables by using typeof() function. you can get the size of the vector using length().


# Types of Vectors
> typeof(id)
#[1] "double"

> typeof(name)
#[1] "character"

> typeof(dob)
#[1] "double"

2. Create Named Vector

You can also assign names to values while creating a vector, if you have names it is referred to as a named vector. The following example creates a vector with the names C1, C2, and C3.


# Create Named Vector
x <- c(C1='A',C2='B',C3='C')
print(x)

# Output
# C1  C2  C3 
#"A" "B" "C" 

3. Create a Vector from the List

If you have a list, you can easily create a vector from a list in R by using unlist() function. This function takes the list as an argument and convert it to vector. By using is.vector() check if the converted vector is of type vector.


# Create Vector from List
li <- list('A','B','C')
v <- unlist(li)
print(v)
print(typeof(v))
print(is.vector(v))

# Output
#[1] "A" "B" "C"
#[1] "character"
#[1] TRUE

4. Vector of Zeros

To create a vector of zeros use the integer() function, this function takes syntax integer(length) where the length param specifies the number of zeros you want to have on the vector.


# Create Vector of Zeros
v <- integer(6)
print(v)

# Output
#[1] 0 0 0 0 0 0

5. Vector of Length N

Let’s say you wanted to create a vector in R of a specified length N with the default values. The above example creates a numeric vector with the value 0 and the specified length. Similarly, to create a character vector with the specified empty spaces use the character(N).


# Create Vector of Specified length
v <- character(5)
print(v)

# Output
#[1] "" "" "" "" ""

6. Vector of 1 to 10

If you want a vector with 1 to 10 sequence numbers use either seq(1,10) function or use 1:10.


# Create Numeric Vector with 0 to 10 Values
v <- 1:10
v <- seq(1, 10)
print(v)

# Output
# [1]  1  2  3  4  5  6  7  8  9 10

7. Using Vector()

The vector() function is used to create a vector of any type. It takes the parameter mode and length. mode is used to specify the type and length is used to specify the length of the vector with default values. The following example creates a logical vector with 5 elements.


# Create Vector using vector()
x <- vector(mode='logical',length=5)
print(x)
print(is.vector(x))
print(typeof(x))

# Output
#[1] FALSE FALSE FALSE FALSE FALSE
#[1] TRUE
#[1] "logical"

8. Complete Example of Create Vector

Following is a complete example of different ways to create a vector in R. You can find the complete example from this article at Github R Programming Examples Project.



# Create Vector using c()
id <- c(10,11,12,13)
name <- c('sai','ram','deepika','sahithi')
dob <- as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16'))

# Create Named Vector
x <- c(C1='A',C2='B',C3='C')

# Create Vector using vector()
x <- vector(mode='logical',length=5)

# Create Character Vector
x <- character(5)

# Create Vector From List
li <- list('A','B','C')
v <- unlist(li)

# Create Vector of Zeros
v <- integer(6)

# Create Vector of Specified length
v <- character(5)

# Create Numeric Vector with 0 to 10 Values
v <- seq(1, 10)
v <- 1:10

# Create Vector using vector()
x <- vector(mode='logical',length=5)

9. Conclusion

In this article, you have learned what is a vector and how to create it by using R primitive c(), vector(), and character() functions and also learned the data types of these created Vectors. You can find the complete example from this article at Github R Programming Examples Project.

Related Articles

References

Exit mobile version