Error: ‘list’ object cannot be coerced to type ‘double’

[This article was first published on Data Analysis in R » Quick Guide for Statistics & R » finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Error: ‘list’ object cannot be coerced to type ‘double’ appeared first on finnstats.

If you want to read the original article, click here Error: ‘list’ object cannot be coerced to type ‘double’.

Error: ‘list’ object cannot be coerced to type ‘double’, as the first step, we’ll need to generate some data that we’ll later use in the examples.How to Calculate Cross-Correlation in R » finnstats

List <- list(1:5,10,10:20)
List
[[1]]
[1] 1 2 3 4 5

[[2]]
[1] 10

[[3]]
 [1] 10 11 12 13 14 15 16 17 18 19 20

The structure of our example data is shown in the previous RStudio console output: It’s a list with three numeric list entries in it.

Principal Component Analysis in R » finnstats

Example 1 Reproducing Error: ‘list’ object cannot be coerced to type ‘double’

When lists are transformed into vectors or arrays, this section shows a common error message.

Let’s see the wrong code and reproduce the error message.

as.numeric(List)
Error: 'list' object cannot be coerced to type 'double'

As you can see, the error message from the RStudio console is: (list) object cannot be converted to type ‘double’.

The reason for this is that we can’t use the as.numeric function on a list with multiple list components.

Decision Tree R Code » Classification & Regression » finnstats

After that, we’ll show you how to correct the problem. So don’t stop reading!

Example 2: Appropriate R Code for List to Numeric Vector Conversion

In R, Example 2 shows how to properly convert a list to a numeric vector. Look at the R code below.

Now we can apply to unlist and convert into as.numeric

Data Visualization Graphs-ggside with ggplot » finnstats

as.numeric(unlist(List))    
[1]  1  2  3  4  5 10 10 11 12 13 14 15 16 17 18 19 20

As you can see, we combined the unlist and as.numeric functions, which means we first converted our list to a vector and then converted that vector to numeric. Everything is in order!

Power analysis in Statistics with R » finnstats

To read more visit Error: ‘list’ object cannot be coerced to type ‘double’.

If you are interested to learn more about data science, you can find more articles here finnstats.

The post Error: ‘list’ object cannot be coerced to type ‘double’ appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Data Analysis in R » Quick Guide for Statistics & R » finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)