Error In .data.frame Frame Predictors Undefined Columns Selected
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Regression tree in R up vote 10 down vote favorite 2 I am having trouble making a regression tree in R. I have a data frame with 17 attributes library(rpart) rt.model <- rpart(razlika ~ ., learn) I get an error: Error in `[.data.frame`(frame, predictors) : undefined columns selected Seems weird because I did something like that with a very simillar database. You can dowload the dataframe on http://uploading.com/files/de8a966d/exa.Rda/ - then load with load("exa.Rda") r regression rpart share|improve this question edited Jan 29 '13 at 9:13 rcs 35.9k10118127 asked Dec 5 '11 at 16:52 Borut Flis 2,543144881 add a comment| 3 Answers 3 active oldest votes up vote 13 down vote accepted The problem is not, I believe, that you have a matrix rather than a data frame. When I download and then load you data set, I get a data frame, not a matrix. The problem is that you have bad characters in the column names. Use gsub to remove the characters "-", " ", "(" and ")" from the column names. Or you can simply redefine the column names yourself entirely using colnames. Or do as ulvund does and simply call data.frame, which forces R to do the column name cleaning for you, by default. When I do this, rpart runs just fine. share|improve this answer answered Dec 5 '11 at 18:33 joran 102k11215270 make.names from the base library can also be used to make syntactically valid names out of character vectors. –Alex Dec 7 '11 at 14:42 add a comment| up vote 3 down vote Turn your learn matrix into a data frame. Example: load("exa.Rda") library(rpart) learn <- data.frame(learn) rt.model <- rpart(razlika ~ ., learn) rt.model yields: n= 226 node), split, n, deviance, yval * denotes terminal node 1) root 226 31417.5100 3.3849560 2) B.reb>=40.80799 117 12661.2300 0.4871795 4) B.ft>=0.7666193 31 2685.4190 -5.7741940 8) A.fg2< 0.4645683 22 1846.7730 -8.3181820 16) A.ft< 0.7464692 7 365.4286 -14.2857100 * 17) A.ft>=0.7
view ♦ ♦ | Report Content as Inappropriate ♦ ♦ rpart space in column names Hi, Our data has column names with spaces in that.The names in dataFrame are, [1] "Sepal Length" "Sepal Width" "Petal Length" "Petal Width" "Species" When i try to use the column names in rpart function, it gives the following error. rp<-rpart(as.factor(`Species`)~`Sepal Length`) Error in `[.data.frame`(frame, predictors) : undefined columns selectedBut , a similar call works for kmeans/nnet functions.For example, nn<-nnet(as.factor(`Species`)~`Sepal Length`,size=3)Is there any way in which column names with spaces be used in rpart function like http://stackoverflow.com/questions/8388791/regression-tree-in-r being used in nnet/kmeans function? Thanks in advance for your help, Raji Jeff Newmiller Threaded Open this post in threaded view ♦ ♦ | Report Content as Inappropriate ♦ ♦ Re: rpart space in column names It is isn't easy to write code that works with column names that have spaces. You could rewrite rpart, or just rename the columns in your data frame to work http://r.789695.n4.nabble.com/rpart-space-in-column-names-td4631557.html around the "bug". See ?names. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<[hidden email]> 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. Raji <[hidden email]> wrote: >Hi, > >Our data has column names with spaces in that.The names in dataFrame >are, > *[1] "Sepal Length" "Sepal Width" "Petal Length" "Petal Width" >"Species" * >When i try to use the column names in rpart function, it gives the >following >error. >* rp<-rpart(as.factor(`Species`)~`Sepal Length`) >Error in `[.data.frame`(frame, predictors) : undefined columns >selected* > >But , a similar call works for kmeans/nnet functions.For example, > *nn<-nnet(as.factor(`Species`)~`Sepal Length`,size=3)* > >Is there any way in which column names with spaces be used in rpart >function >like being used in nnet/kmean
invalid column names (such https://stat.ethz.ch/pipermail/r-help/2011-October/294208.html as all numbers). Try using make.names() on the datasets. Max On Oct 30, 2011, at 11:35 AM, Luisa Sêco