R Apply Family Loops (like for, while and repeat) are a way to repeatedly execute some code. However, they are often slow in execution when it comes to processing large data sets. R has a more efficient and quick approach to perform iterations – The apply family. Apply family in R The apply family consists of vectorized functions. Below are …
Month: May 2021
R Functions In simple terms, a function is a block of statements that can be used repeatedly in a program. R provides many built-in functions and allows programmers to define their own functions. Syntax Here’s the syntax of a function in R: Create a Function To define a function in R, use the function command and assign the results …
R Repeat Loop A repeat loop just repeats a block of code indefinitely. In this loop, no condition checking is performed in order to end the loop. To stop repeating the loop, you must put a condition explicitly inside it with break statement. Failing to do so will create an infinite/endless loop. Syntax Here’s the syntax of the repeat …
R For Loop The for statement in R is a bit different from what you usually use in other programming languages. Rather than iterating over a numeric progression, R’s for statement iterates over the items of a vector or a list. The items are iterated in the order that they appear in the vector. Syntax Here’s the syntax of the for statement: Basic Examples …
R While Loop A while loop is used when you want to perform a task indefinitely, until a particular condition is met. It’s a condition-controlled loop. Syntax Here’s the syntax of the while statement: Basic Examples Any non-zero value or nonempty container is considered TRUE; whereas Zero, None, and empty container is considered FALSE. # Iterate until x becomes …
R if else elseif Statement Often, you need to execute some statements only when some condition is met. You can use following conditional statements in your code to do this. if Statement: use it to execute a block of code, if a specified condition is true else Statement: use it to execute a block of code, if …
R Factor In real-world problems, you often encounter data that can be classified in categories. For example, suppose a survey was conducted of a group of seven individuals, who were asked to identify their hair color and gender. The result might appear as follows: Name Hair color Gender Amy Blonde Female Bob Black Male Eve …
R Data Frame Suppose you want to store the names of your employees, their age and addresses all in one dataset. The first thing that readily comes to mind is Matrix. But you can’t combine all this data in one matrix without converting it to character data. So, you need a new data structure to keep …
R Matrix A matrix is a collection of elements, all the same type, arranged in a two-dimensional layout. In a nutshell, a matrix is just a vector that has two dimensions. When using R, you will frequently encounter the four basic matrix types viz. logical, character, integer and double (often called numeric). Create a Matrix You can …
R List Vectors and matrices are incredibly useful data structure in R, but they have one distinct limitation: they can store only one type of data. Lists, however, can store multiple types of values at once. A list can contain a numeric matrix, a logical vector, a character string, a factor object and even another list. Create a …