Here we are explaining how to write an R program to iterate over all the elements of a vector and print the current value. Here we are using for loop for the iteration. The syntax of sorting is like
Here, the sequence may be a vector and val takes on each of its values during the loop. In each iteration of the loop, the statement is evaluated.
Below are the steps used in the R program to iterate over all the elements of a vector and print the current value. In this R program, we directly give the values iterative for loop. The variable fruit indicating the vector of fruits. iterate through vector fruit using the variable i. In each iteration print i th value.
# Create fruit vector
fruit <- c('Banana', 'Orange', 'Mango', 'Apple')
# Create the for statement
for(i in fruit){
print(i)
}
## [1] "Banana"
## [1] "Orange"
## [1] "Mango"
## [1] "Apple"
# Create item vector
item <- c('Paper', 'Pen', 'Pencil', 'Board')
# Create the for statement
for(i in item){
print(i)
}
## [1] "Paper"
## [1] "Pen"
## [1] "Pencil"
## [1] "Board"