Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)

The code:

a <- structure(list(`X$Days` = c("10", "38", "66", "101", "129", "185", "283", 

                                 "374")), .Names = "X$Days")

Then a is like

$`X$Days`

[1] "10"  "38"  "66"  "101" "129" "185" "283" "374"

I would like to coerce a to an array of numeric values, but coercing functions return me

Error: (list) object cannot be coerced to type 'double'

Thanks,

closed

1 Answer

0 votes
by
edited
 
Best answer

To convert a list object to numeric, you can use the unlist() function to convert it to a vector, and then use as.numeric() to convert it into a vector.i.e.,

a <- structure(list(`X$Days` = c("10", "38", "66", "101", "129", "185", "283", 

                                  "374")), .Names = "X$Days")

class(unlist(a))

[1] "character"

as.numeric(unlist(a))

[1]  10  38  66 101 129 185 283 374

If you want to explore more in R programming then watch this R programming tutorial for beginner:

Browse Categories

...