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", 10)
print("Numeric Type:")
## [1] "Numeric Type:"
print(N)
##  [1] 0 0 0 0 0 0 0 0 0 0
C = vector("complex", 10)
print("Complex Type:")
## [1] "Complex Type:"
print(C)
##  [1] 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i
L = vector("logical", 10)
print("Logical Type:")
## [1] "Logical Type:"
print(L)
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Chr = vector("character", 10)
print("Character Type:")
## [1] "Character Type:"
print(Chr)
##  [1] "" "" "" "" "" "" "" "" "" ""