How to Sum List Items in R (2 Examples)

In this R tutorial you’ll learn how to sum list elements.

Example Data

x <- list(c(7, 3, 8),        # Constructing list in R
          c(8, 3, 6),
          9:7)
x
# [[1]]
# [1] 7 3 8
# 
# [[2]]
# [1] 8 3 6
# 
# [[3]]
# [1] 9 8 7

Example 1: Get Sum Within List Item

sapply(x, sum)               # Using sapply function
# [1] 18 17 24

Example 2: Get Sum Between Different List Items

Reduce("+", x)               # Using Reduce function
# [1] 24 14 21

Related Tutorials & Further Resources

You may find some related R tutorials on topics such as loops, extracting data, and lists in the following list:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top