3

I just started using R. I am supposed to Calculate a new variable “Vehic_vol” from the sum of “Psgr_Vol” and “Lugg_Vol” and Plot this new variable against “CITY_MPG” for the whole data set but I end up with 'x' and 'y' lengths differ ERROR! Any thoughts?

Here is what I did:

Vehic_vol<-(VehicleData$Psgr_Vol + VehicleData$Lugg_Vol)
 plot (VehicleData$Vehic_vol, VehicleData$CITY_MPG)
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ
HK boy
  • 1,398
  • 11
  • 17
  • 25
Farshad
  • 37
  • 1
  • 1
  • 7

2 Answers2

6

From the code you provided, Vehic_vol is not a column of VehicleData. If you enter in

VehicleData$Vehic_vol

it returns

NULL

Note that NULL and VehicleData$CITY_MPG have different lengths (use length() to verify that).

Try this instead

plot (Vehic_vol, VehicleData$CITY_MPG)

or

VehicleData$Vehic_vol <- (VehicleData$Psgr_Vol + VehicleData$Lugg_Vol)
plot (VehicleData$Vehic_vol, VehicleData$CITY_MPG)
blakeoft
  • 2,370
  • 1
  • 14
  • 15
  • **General rule suggestion**: In my R code I tend to separate a _"add derived values to my dataframe"_ phase from a _"now use the extended stuff for plotting"_ phase. In this manner of working you would have written `VehicleData$Vehic_vol <- (VehicleData$Psgr_Vol + VehicleData$Lugg_Vol)` (variant 2 of above) and the subsequent `plot` command would have worked as you wanted it to as a matter of course (without straining your short term memory much). – Lutz Prechelt Jan 16 '15 at 18:54
-2

For Example, my dataset,

mod.2 <- lm(CEC ~ clay + ExchNa + ExchCa,
             data = subs.soil.data)

when you write a model like this and want to draw this modela plot, plot(mod.2$y, mod.2$fitted.values) this is error "Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ"

--First, check with length ()

length(mod.2$y)
[1] 0

As you can see, the dependent variable y is 0 in length, i.e. it doesn't exist.

Solution mod.2 <- lm (CEC ~ clay + ExchNa + ExchCa, data = subs.soil.data, y = TRUE, x = TRUE) We did not define x and y in the previous formula for mod.2, and this is the cause of the error.

plot(mod.2$y, mod.2$fitted.values)
> length(mod.2$y)
[1] 146
> length(mod.2$fitted.values)
[1] 146
> 

This problem has now disappeared.

IRTFM
  • 258,963
  • 21
  • 364
  • 487