Control Structures in R
Control Structures in R
Structures in R
Dr. Rakhee Chhibber
Taking input in R
There are two methods in R.
# string input
var1 = readline(prompt = "Enter your name : ");
# character input
var2 = readline(prompt = "Enter any character : ");
# convert to character
var2 = as.character(var2)
# printing values
print(var1)
print(var2)
Using scan() method
Another way to take user input in R language is using a method,
called scan() method. This method takes input from the console.
This method is a very handy method while inputs are needed to
taken quickly for any mathematical calculation or for any dataset.
This method reads data in the form of a vector or list. This method
also uses to reads input from a file also.
# R program to illustrate
# taking input from the user
Explanation:
Total 12 integers are taking as input in 2 lines when the
control goes to 3rd line then by pressing Enter key 2 times
the input process will be terminated.
double, string, character type
values using scan() method
To take double, string, character types inputs, specify the type of the
inputted value in the scan() method. To do this there is an argument
called what, by which one can specify the data type of the inputted
value.
# R program to illustrate
# taking input from the user
x <- "GeeksforGeeks"
x = "GeeksforGeeks" # string
x1 = 255 # integer
x2 = 23.14 # float
# string print
sprintf("%s is best", x)
# integer print
sprintf("%d is integer", x1)
# float print
sprintf("%f is float", x2)
Print output
using cat() function
Another way to print output in R is using of cat() function. It’s same
as print() function. cat() converts its arguments to character
strings. This is useful for printing output in user defined functions.
# R program to illustrate
# printing output of an R program
# print string with variable
# "\n" for new line
x = "GeeksforGeeks"
cat(x, "is best\n")
# print normal string
cat("This is R language")
Introduction
Control Structures are an integral part of any programming language. We can say
that a programming language without control structures is incomplete. These
control structures are basically a block of code lines that decide which direction
to go once the executed and often called as well as considered as loops. Control
structures are mostly used for decision making.
• If – else statement
• ifelse() function
• Switch Statement
• for loop
• while loop
• Break Statement
• Next Statement
• Report Loops
If..condition
x <- 100
x <- 5
x <- letters[4:10]
for(i in x){
print(i)
}
Nested loops
# Defining matrix
m <- matrix(2:15, 2)
for (r in seq(nrow(m))) {
for (c in seq(ncol(m))) {
print(m[r, c])
}
}
While loop
x=1
# Print 1 to 5
while(x <= 5){
print(x)
x=x+1
}
repeat loop and break
statement
repeat is a loop which can be iterated many number of times but
there is no exit condition to come out from the loop. So, break
statement is used to exit from the loop. break statement can be
used in any type of loop to exit from the loop.
x=1
# Print 1 to 5
repeat{
print(x)
x=x+1
if(x > 5){
break
}
}
return statement
# Checks value is either positive, negative or
return statement is used zero
to return the result of an func <- function(x){
executed function and if(x > 0){
returns control to the return("Positive")
calling function. }else if(x < 0){
return("Negative")
}else{
return("Zero")
}}
#Function calling
func(1)
func(0)
func(-1)
next statement
next statement is used to skip # Defining vector
the current iteration without x <- 1:10
executing the further
statements and continues the # Print even numbers
next iteration cycle without for(i in x){
terminating the loop. if(i%%2 != 0){
next #Jumps to next loop
}
print(i)
}
Switch case
val1 = 6
val2 = 7
val3 = "s"
result = switch(
val3,
"a"= cat("Addition =", val1 + val2),
"d"= cat("Subtraction =", val1 - val2),
"r"= cat("Division = ", val1 / val2),
"s"= cat("Multiplication =", val1 * val2),
"m"= cat("Modulus =", val1 %% val2),
"p"= cat("Power =", val1 ^ val2)
)
print(result)