Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting in R; cannot be coerced to double error

Tags:

r

graphing

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.

like image 811
Concerned_Citizen Avatar asked Jul 14 '11 01:07

Concerned_Citizen


1 Answers

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:

?'['
like image 177
Brandon Bertelsen Avatar answered Sep 23 '22 02:09

Brandon Bertelsen