Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

iterations
vectors
dataframes
r
r language

What are the different types of iterations in R?

Marvah Mushtaq

Overview

In R, iteration is not preferred because it’s computationally expensive.

Apart from the basic looping functions like for and while, R also supports the following vectorized looping functions:

  • apply()
  • lapply()
  • tapply()
  • sapply()
  • by()

The primary goal of these functions is to avoid using loop structures explicitly. They can apply a function on an input list, matrix, or array. These functions accept any function as a parameter.

We’ll be looking into three main functions in detail in this shot.

The apply() function

The apply() function accepts a data frame, vector, list, or array as input and returns a data frame, vector, list, or array as output.

Syntax

apply(X, MARGIN, FUN)

Arguments

  • X: This is a data frame, vector, list, or array.
  • MARGIN: This takes a value or range between 1 and 2 to define where to apply the function, e.g.:
    1. MARGIN=1: The manipulation is performed on rows.
    2. MARGIN=2: The manipulation is performed on columns.
    3. MARGIN=c(1,2) The manipulation is performed on rows and columns.
  • FUN: This specifies which function should be used. Mean, median, sum, min, max, and other built-in functions, as well as user-defined functions, can be used.

Code

Let’s look at the following example.

df <- data.frame(x = 1:4, y = 5:8, z = 10:13)

#df x y  z
#   1 5 10 = 16 (sum by row)
#   2 6 11 = 19 (sum by row)
#   3 7 12 = 22 (sum by row)
#   4 8 13 = 25 (sum by row)

# Sum by row
apply(X = df, MARGIN = 1, FUN = sum)

Explanation

  • Line 1: This defines the data frame named df.
  • Line 10: This outputs the summed by row values as shown in lines 3-7.

The lapply() function

The lapply() function can be used to perform operations on a list or vector that returns a list or vector of the same length as the original with the resultant elements obtained after applying the specified FUN.

Syntax

lapply(X, FUN)

Arguments

  • X: This is a list or vector.
  • FUN: This is the function to be applied on each element of X.

Code

Let’s look at the following example.

movies <- c("SPYDERMAN","BATMAN","VERTIGO","CHINATOWN")
movies_lower <- lapply(movies, tolower)
str(movies_lower)

Explanation

  • Line 1: This defines the vector named movies.
  • Line 2: This applies the tolower function and stores the results to movies_lower.
  • Line 3: This displays the structure of movies_lower.

The sapply() function

The sapply() function accepts a vector, list, or array as input and returns a vector, list, or array as output.

Syntax

sapply(X, FUN)

Arguments

  • X: This is a vector, list, or array.
  • FUN: This is a function applied to each element of X.

Code

Let’s look at the following example.

data <- list(item1 = 1:5,
             item2 = seq(4,36,8),
             item4 = c(1,3,5,7,9))

# $item1
# [1] 1 2 3 4 5 = 15 (summed)
# $item2
# [1]  4 12 20 28 36 = 100  (summed)
# $item4
# [1] 1 3 5 7 9 = 25  (summed)

sapply(data, sum)

Explanation

  • Lines 1-3: This defines the list named data.
  • Line 12: This sums and displays the values of data as shown in lines 5-10.

RELATED TAGS

iterations
vectors
dataframes
r
r language

CONTRIBUTOR

Marvah Mushtaq
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring