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_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' |
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 |
as.numeric(unlist(example_list)) # Applying valid code # 5 6 7 4 1 3 8 2 6 20 19 18 17 16 15