R – error “variable lengths differ”

This happens because in your first step you created a separate variable outside of your data frame, transLOT<-log(LengthofTimemin). When you remove a row from the data, transLOT is unchanged. Even worse than differing lengths, your data doesn’t line up any more – if the different lengths were ignored, your rows of data would be “off by one” compared to the response after the row you removed.

The simple solution is to create your transLOT variable in the data frame. Then, whenever you do things to the data (like remove rows), the same thing is done to transLOT.

resdata$transLOT <- log(resdata$LengthofTimemin)

Note that I also use the resdata$LengthofTimemin rather than LengthofTimemin which you seem to have in your workspace. Did you use attach() at some point? You shouldn’t use attach for exactly this reason. Keep variables in the data frame!

Leave a Comment