Plotting in R; cannot be coerced to double error

Concerned_Citizen picture Concerned_Citizen · Jul 14, 2011 · Viewed 23.9k times · Source

I am trying to plot a and b, each consisting of 7500 data points. However when I tried plot(x,y), I got the following error:

> plot(a[11],b[11])
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  (list) object cannot be coerced to type 'double'

Which is strange,because all values all whole numbers. What can I do?

Thank you.

Answer

Brandon Bertelsen picture Brandon Bertelsen · Jul 14, 2011

It looks like you're trying to plot a vector from a list. Try subseting using $ or [[]] instead.

Here's your problem:

a <- as.list(data.frame("x"=1:5,"y"=5:1))
b <- as.list(data.frame("x"=1:5,"y"=5:1))

plot(a[2],b[2]) ## recreates your error

Here's the solution:

plot(a$y, b$y) ## plots as expected subsetting by $

Alternatively, if you'd prefer to stick with numbers:

plot(a[[2]],b[[2]])

I would strongly recommend that you read the help page associated with this:

?'['