Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label Delete Vector. Show all posts
Showing posts with label Delete Vector. Show all posts

Saturday, 18 August 2018

R Vectors - Create, Access, Modify and Delete a Vector Elements in R

 A Vector in R is basically a set of values of the same basic data type like numeric character etc. A vector in R is created using the c() function that represents a combination of elements.

I have posted basics about R Vectors in the previous post, here we'll learn more about Vector data type.



Creating a Vector in R

In R even a single value is considered as a vector of length 1, We can create a multi-element R Vector using a colon (:) like this

  1. #Creating a vector using a colon
  2. v <- (1:10)
  3. #Print its values
  4. print(v)

and output is

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

Create a vector using c() function

  1. # A Numeric Vector
  2. numeric_vector <- c(10, 20, 30)
  3. # A Character Vector
  4. character_vector <- c("a", "b", "c")
  5. # A Boolean Vector
  6. boolean_vector <-c(TRUE,FALSE,TRUE)

now print these vectors

  1. > #Print All Vectors
  2. > print(numeric_vector)
  3. [1] 10 20 30
  4. > print(character_vector)
  5. [1] "a" "b" "c"
  6. > print(boolean_vector)
  7. [1] TRUE FALSE TRUE

Accessing Vector Elements in R

Vectors in R works on the concept of the index and to access vector elements value we need to use the index, Indexing in R starts from 1 and here we'll see how to access a vector's value.

  1. #Define a Vector
  2. v <- c(10,20,30,40,50,60,70,80)
  3. #Get element's value using index
  4. a <- v[c(1,3,4,6)]
  5. #Print values
  6. print(a)

and output is

  1. [1] 10 30 40 60

We can also use boolean values (TRUE, FALSE) to get elements of the vector, See an example here

  1. #Define a Vector
  2. v <- c(10,20,30,40,50,60,70,80)
  3. #Get element's value using boolean value
  4. a <- v[c(TRUE,FALSE,FALSE,FALSE,TRUE,TRUE,FALSE,TRUE)]
  5. #Print values
  6. print(a)

and output is

  1. [1] 10 50 60 80

Using negative value as index removes that particular element from the result, see an example

  1. #Define a Vector
  2. v <- c(10,20,30,40,50,60,70,80)
  3. #Get element's value using negative index
  4. a <- v[c(-2,-4,-6,-8)]
  5. #Print values
  6. print(a)

and output is

  1. [1] 10 30 50 70

Arithmetic Operation in Vectors

We can perform variables like arithmetic operations (Addition, Subtraction, Multiplication, Divison) in vectors too.

  1. #Add two vectors
  2. a <- c(1, 2, 3)
  3. b <- c(4, 5, 6)
  4. # Addition
  5. c <- a+b
  6. # Subtraction
  7. d <- a-b
  8. # Multiplication
  9. e <- a*b
  10. # Divison
  11. f <- a/b

now on printing this

  1. > #Print Values
  2. > cat("Addition",c)
  3. Addition 5 7 9>
  4. > cat("Subtraction",d)
  5. Subtraction -3 -3 -3>
  6. > cat("Multiplication",e)
  7. Multiplication 4 10 18>
  8. > cat("Divison",f)
  9. Divison 0.25 0.4 0.5>

Modifying a Vector in R

We can modify vector’s elements values using index and assignment operator. See this example

  1. #Define a Vector
  2. v <- c(10,20,30,40,50,60,70,80)
  3. #Modify 3rd element value
  4. v[3] <- 33;
  5. #Print Updated Vector
  6. print(v)

and output is

  1. [1] 10 20 33 40 50 60 70 80

Deleting a Vector in R

We can delete a vector by setting a vector to NULL.

  1. #Define a Vector
  2. v <- c(10,20,30,40,50,60,70,80)
  3. #Set its value to NULL
  4. v <- NULL
  5. #Print Vector
  6. print(v)

Cheers 🙂 Happy Learning