R Program to create vector of numeric, complex, logical and character types of length 6

How to create a vector of numeric, complex, logical, and character

Here we are explaining how to write an R program to create a vector of numeric, complex, logical, and character types of length 6. Here we are using the built-in function vector() for the creation of vectors. Using this function we can create a vector of any type. The syntax of this function is 

vector(mode = "logical", length = 0)
 

In this 'mode' indicates the character string naming an atomic mode or "list" or "expression" or "any". And 'length' is used for a non-negative integer specifying the desired length.

How to create a vector of numeric, complex, logical, and character using the R Program

Given below are the steps which are used in the R program to create a vector of numeric, complex, logical, and character types of length 6. In this R program, we accept the vector values into variables N, C, L, Chr. Variable N is for saving numeric vector, C is for complex vector, L is for Logical vector finally Chr for holding character vector. These vectors are created by calling the vector() method with type and length as its arguments. Finally, each vector is printed as result.

ALGORITHM

STEP 1: take the variables N, C, L, Chr for holding vectors of different types 

STEP 2: first create numeric vector N as N= vector("numeric", 5)

STEP 3: print vector N

STEP 4: create complex vector C as C= vector("complex", 5)

STEP 5: print vector C

STEP 6: create logical vector L as L= vector("logical", 5)

STEP 7: print vector L

STEP 8: create characte vector Chr as Chr= vector("character", 5)

STEP 9: print vector Chr

R Source Code

                                          N= vector("numeric", 5)
print("Numeric Type:")
print(N)
C= vector("complex", 5)
print("Complex Type:")
print(C)
L= vector("logical", 5)
print("Logical Type:")
print(L)
Chr= vector("character", 5)
print("Character Type:")
print(Chr)
                                      

OUTPUT

[1] "Numeric Type:"
[1] 0 0 0 0 0
[1] "Complex Type:"
[1] 0+0i 0+0i 0+0i 0+0i 0+0i
[1] "Logical Type:"
[1] FALSE FALSE FALSE FALSE FALSE
[1] "Character Type:"
[1] "" "" "" "" ""