(list) object cannot be coerced to type ‘double’

The essence of the problem

This error frequently shows up when you’ve got an list of numeric strings which you want R to treat as numbers. Since these are most assuredly not numbers already, R falters and loudly proclaims: (list) object cannot be coerced to type ‘double’. Thus the error and why you’re here (most likely).

When confronted with a list that contains things which are “not numbers”, in R’s narrow view of the world, the system is throwing this error to ask for some clarity in the matter. Why the fixation on ‘double’ as the target data type ? That’s the default numeric type that R apparently likes. Speaking from practical experience, that particular data type works for most common data sets.

From practical experience, this is less disconcerting that it seems. It is not uncommon for “things” to lurk in large databases, especially databases of international consumer data that were not properly maintained, that do not cast themselves gracefully into numbers if asked to do so. Caution is prudent. This is actually a good thing – we just need to force a little clarity into the act of the conversion.

Fixing This Issue

I recommend politely suggesting to your script that it treat the values in question as numeric. Since verbal approaches are notoriously ineffective, this requires a small correction to your code.

You will want to use the as.numeric function. This will convert the (likely) text values contained within your list into appropriate numeric values.

I should note that the as.numeric() function expects a single element or vector as input, so you may need to adjust your code to apply it properly to each element. Potential ways to use this function:

  • Use the unlist() function to convert your list into a single vector and feed the result into as.numeric. It will deliver a list of numeric values for your inspection.
  • You can use the lapply function to apply the as.numeric function to each elements of the list
  • You can use list sub-setting to target specific elements of the list and feed them into the as.numeric function.

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

Scroll to top