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


February 8, 2023, Learn eTutorial
1581

How to create a vector of numeric, complex, logical, and character types using R program?

For writing an R program to create a vector of numeric, complex, logical, and character types of length 6, we are using the built-in function vector(). By using this function in R, we can create a vector of any type. The syntax of this function is 

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

where,

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

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. Each of these vectors is created by calling the vector() function 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 character 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] "" "" "" "" ""