0% found this document useful (0 votes)
26 views4 pages

R Statements (If, Loop, Etc.)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

R Statements (If, Loop, Etc.)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

5. R Statements (if, loop, etc.

Similar to any programming language, R also provides statements like if, loop,
while, and repeat e.t.c

5.1 Statement if… else

The R language supports three different ways to write if else statement , The if,
if…else, and if…else…if in R. These if statements are conditional decision-
making statements that are used to execute a block of code based on a
condition.
Following are some examples of an if statement with a condition. You can also
write R if..else with multiple conditions .
# R if statement
str <- 'Spark'
if(str == 'Spark') {
print('IF CONDITION IS TRUE')
}

# R if...else statement
str <- 'Python'
if(str == 'Spark') {
print('IF CONDITION IS TRUE')
}else{
print('IF CONDITION IS FALSE')
}

# R if...else if
str <- 'Python'
if(str == 'Spark') {
print('str value is Spark')
}else if(str == 'Python' ) {
print('str value is Python')
}else{
print('str value is not Spark and Python')
}
Alternatively, you can also use ifelse() function from the R base package that
takes a vector as input and return a vectorized output. The ifelse() is a function
that takes a vector as a test condition and executes the test condition for each
element in th
5.2 for loop

The for loop in R is used to repeatedly execute a set of statements or block of


code for every element in a sequence (vector, list, array etc.).
# R for loop
numbers <- c('One','Two',"Three","Four","Five")
for(i in numbers ) {
print(i)
}
To interrupt the looping statements you can use the break and next statements
in R.
Following are some other examples of using a for loop.

 Nested For Loop in R


 Run R for Loop in Parallel
5.3 while loop

The while loop in R is used to execute a set of statements in a loop as long as a


condition is TRUE. It is a control statement.
# while example
i <- 1
n <- 5
while (i <= n) {
print(i)
i = i + 1
}
5.4 repeat loop

The repeat loop statement in R is similar to do while statement in other


languages. repeat run the block of statements repeatedly until the break jump
statement is encountered. You have to use the break statement to terminate or
exit the loop. Not using a break will end up the repeat statement in an indefinite
loop.
# repeat example
i = 1
repeat {
print(i)
i = i + 1
if(i >= 5 )
break
}

6. Run R script Using rscript Command

Running R programs from RStudio is beneficial during development, as it


allows you to execute statements and validate the output interactively.
However, in real-time applications, R programs are typically written in R script
files with the extension .r and executed from the command line.

Open your preferred text editor and create a file named helloworld.r. Include
the data frame and print statements as described in section 2.

R script file

Now, open the terminal or command prompt and run the R script file using
the rscript command. If the file is stored in a custom location, use the absolute
path of the script to execute it. You can also create an R program in RStudio,
save the file to the disk, and run it using the rscript command.

Execute rscript from the command line


7. R Base Functions

There are several built-in R base functions


8. R Packages

To use these packages, you have to install R packages first


using install.packages('<package>') and load them
using library(<package>) . Below are some of the most used R packages to
learn and explore. Click on each item below to get to the tutorial for each R
package.
 dplyr
 tidyr
 data.table
 tidyverse
 stringr
 tibble
 sparkly
 sqldf
If you already have these packages and to update to the latest version
either remove the package and install it again or update it using
update.packages()
9. Conclusion

In this R Programming Tutorial, you have learned what is R, its usage, and how
to install and run the Hello World program. Also, it learned data structures it
supports like Vector, Matrix, and Data Frame. Finally looked into different R
packages.

You might also like