'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
0

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)
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest