Please disable your adblock and script blockers to view this page

Search this blog

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

Tuesday 14 August 2018

 Loops (R Loops) are used to repeat the execution of a block of statements based on some predefined conditions. In R Programming we have the following types of loops that can be used as per development requirements.



Repeat Loop in R Programming

Repeat loop in R executes the set of statements until the defined condition is satisfied

  1. #Print some value using Repeat Loop
  2. count <- 1
  3. repeat {
  4. print("R Programming")
  5. count <- count+1
  6. if(count > 5) {
  7. break
  8. }
  9. }

and output is

  1. [1] "R Programming"
  2. [1] "R Programming"
  3. [1] "R Programming"
  4. [1] "R Programming"
  5. [1] "R Programming"

Another Example

  1. #Print table of 10 using Repeat Loop
  2. a <- 10
  3. count <- 1
  4. repeat {
  5. print(a*count)
  6. count <- count+1
  7. if(count > 10) {
  8. break
  9. }
  10. }
and output is

  1. [1] 10
  2. [1] 20
  3. [1] 30
  4. [1] 40
  5. [1] 50
  6. [1] 60
  7. [1] 70
  8. [1] 80
  9. [1] 90
  10. [1] 100

While Loop in R Programming

While loop checks the condition before starting loop execution and executes the set of statements while the defined condition is true.

  1. #Declare and Initialise a varable
  2. a <- 1
  3. #Check condition in a while loop
  4. while(a<=5){
  5. print(a)
  6. a <- a+1
  7. }

and output is

  1. [1] 1
  2. [1] 2
  3. [1] 3
  4. [1] 4
  5. [1] 5

FOR Loop in R Programming

When you need to execute a code a specific number of times then FOR loop comes into action. Here we'll see how to iterate a vector using FOR loop.

  1. #Declare and Initialise a Vector
  2. a <- c(10,TRUE,"Ashish",2.4)
  3. #Iterate vector using FOR loop
  4. for(i in a){
  5. print(i)
  6. }

and output is

  1. [1] "10"
  2. [1] "TRUE"
  3. [1] "Ashish"
  4. [1] "2.4"

Cheers 🙂 Happy Learning