‘x’ and ‘y’ lengths differ ERROR when plotting

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

2 Answers

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)

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.

Leave a Reply

Your email address will not be published. Required fields are marked *