[R] rpart space in column names

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon May 28 09:49:41 CEST 2012


On 28/05/2012 08:27, Jeff Newmiller wrote:
> 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 around the "bug". See ?names.

In any case, rpart pre-dates the `` notation that made this possible.

Note that this looks very like the iris data set, which does have 
syntactic names.  Not that we have the reproducible code the posting 
guide asked for ....

 From ?formula

      Variable names can be quoted by backticks ‘`like this`’ in
      formulae, although there is no guarantee that all code using
      formulae will accept such non-syntactic names.


> ---------------------------------------------------------------------------
> 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.
>
> Raji<raji.sankaran at gmail.com>  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`)

You don't need `` unless the name is non-syntactic, e.g. contains a space.


>> 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/kmeans function?
>>
>> Thanks in advance for your help,
>> Raji


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list