0 votes
1 view
in R Programming by (5k 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,

1 Answer

0 votes
by (23.2k points)
edited by
 
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:

...