# How to Use the reduce() Function in R

Often you may want to reduce a list in R to a single value by iteratively applying a binary function.

Fortunately this is easy to do by using the reduce() function from the purrr package in R, which is designed to perform this exact task.

The reduce() function uses the following basic syntax:

reduce(.x, .f)

where:

• .x: The name of a list or atomic vector
• .f: A 2-argument function. The function will be passed the accumulated value as the first argument and the “next” value as the second argument

The following example shows how to use the reduce() function from the purrr package in practice.

Note: Before using the reduce() function, you may need to first install the purrr package by using the following syntax:

`install.packages('purrr')`

Once the purrr package is installed, you can use the reduce() function.

## Example: How to Use the reduce() Function in R

Suppose that we would like to define a vector of five values and then use the reduce() function to iteratively multiply each value in the vector until we reach a single value.

We can use the reduce() function with the following syntax to do so:

```#define vector
my_vector <- c(1, 4, 2, 3, 7)

#reduce vector to single value
my_vector %>% reduce(`*`)

[1] 168```

We can see that this returns a value of 168.

In this example we first define the vector named my_vector, then passed it to the reduce() function and specified that the function `*` should be iteratively applied to each element in the vector.

Thus, the following operation was performed: 1 * 4 * 2 * 3 * 7 = 168.

Note that we could also use a different operator in the reduce() function to perform a different calculation. For example, we could use a plus sign instead to add each element in the vector together:

```#define vector
my_vector <- c(1, 4, 2, 3, 7)

#reduce vector to single value
my_vector %>% reduce(`+`)

[1] 17```

We can see that this returns a value of 17.

In this example we first define the vector named my_vector, then passed it to the reduce() function and specified that the function `+` should be iteratively applied to each element in the vector.

Thus, the following operation was performed: 1 + 4 + 2 + 3 + 7 = 17.

Note that we could also define a more complex function to pass to the reduce() function if we would like.

For example, we could define a function that allows us to concatenate each of the values together in the vector, using a dash as a separator:

```#define vector
my_vector <- c(1, 4, 2, 3, 7)

#define function to use in reduce()
paste_values <- function(x, y, sep = "-") paste(x, y, sep = sep)

#reduce vector to single value
my_vector %>% reduce(paste_values)

[1] "1-4-2-3-7"
```

Notice that this returns 1-4-2-3-7, which represents each of the values from the vector concatenated into a single string, using a dash as a separator.

Feel free to define any function that you would like to pass to the reduce() function depending on your end goal.

Note: You can find the complete documentation for the reduce() function from the purrr package here.

## Additional Resources

The following tutorials explain how to perform other common tasks in R: