R How to Fix the Error – Coerce List Object to Type Double (2 Examples)

In this article, I’ll show how to handle the error message “Coerce List Object to Type Double” in R programming.

Creating Exemplifying Data

example_list <- list(5:7,           # Creating list with numeric values
                c(4, 1, 3, 8, 2),
                6,
                20:15)
example_list                        # Printing list to console
# [[1]]
# [1] 5 6 7
# 
# [[2]]
# [1] 4 1 3 8 2
# 
# [[3]]
# [1] 6
# 
# [[4]]
# [1] 20 19 18 17 16 15

Example 1: Error: (list) object cannot be coerced to type ‘double

as.numeric(example_list) # This R syntax leads to an error
# Error: (list) object cannot be coerced to type 'double'

Example 2: Fixing the Error with unlist & as.numeric Functions

as.numeric(unlist(example_list))    # Applying valid code
# 5  6  7  4  1  3  8  2  6 20 19 18 17 16 15

Leave a Reply

Your email address will not be published.

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