[R] Variable Length Differ

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sun Nov 2 19:33:24 CET 2014

This is not reproducible (see for example [1]), so it is very difficult to know exactly what the problem is. Also, you need to post on this list using the plain text format option in your email software, since the HTML format you used can mess up your code.

I can say that using "[" indexing on a data frame will give you back a data frame as long as you have multiple result columns, and cbind converts that to a matrix. Since data frames typically have columns of a variety of types ("storage modes") while matrices do not, you are likely damaging your data frames every time you use cbind. I recommend that you interactively execute (e.g. using copy/paste) one line at a time in your script and use the str() function after each line to learn the structure of the the objects you are creating and modifying. 

The best way to make a data frame from vector is:

test <- data.frame( BSUPred, PressurePred )


test <- data.frame( BSUPred=BSUPred, PressurePred=PressurePred )

A couple of ways to add columns to data frames that don't damage the data frame are:

test$Placer <- i

or (an example where specifying only one column identifies a vector rather than the usual data frame result)

test[ , "Placer" ] <- i

or even (not recommended for efficiency reasons):

test <- data.frame( test, Placer=i )

If this is not enough help, make your example reproducible using the advice in [1] and try posting again.

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
Sent from my phone. Please excuse my brevity.

On November 2, 2014 3:19:45 AM PST, TJUN KIAT TEO <teotjunk at hotmail.com> wrote:
>This is my code
>    BSUPred<-(forecast(BSU,h=h)[[2]])
>    PressurePred<-(forecast(Pressure,h=h)[[2]])
>    Placer<-(rep(1,h))
>    test<-as.data.frame(cbind(BSUPred,PressurePred))
>    test$Placer<-rep(1:2,h/12)
>    test$Placer<-i
>    test<-as.data.frame((test[c("Placer","BSUPred","PressurePred")]))
>    resp<-predict(fit,newdata=test,type="response")
>    Pred<-ifelse((predict(fit,newdata=test,type="response")>0.5),1,0)
>But I get this error message
>Error in model.frame.default(Terms, newdata, na.action = na.action,
>xlev = object$xlevels) : 
>  variable lengths differ (found for 'Placer') 
>Can anyone help?
>Tjun Kiat
>	[[alternative HTML version deleted]]
>R-help at r-project.org mailing list
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list