[R] Dealing with data frame column names beginning with a numeric

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Apr 16 23:12:42 CEST 2007


On Mon, 16 Apr 2007, Duncan Murdoch wrote:

> On 4/16/2007 4:22 PM, Patrick Connolly wrote:
>> I wish to set up a simple function using boxplot so that it will be
>> available to someone using R for Windows.  (I myself use Linux.)
>>
>> The way the data is organised makes it convenient to use the boxplot
>> function in a manner similar to this example given in the help.
>>
>>
>>>      mat <- cbind(Uni05 = (1:100)/21, Norm = rnorm(100),
>> +                   T5 = rt(100, df = 5), Gam2 = rgamma(100, shape = 2))
>>>      boxplot(data.frame(mat), main = "boxplot(data.frame(mat), main = ...)")
>>
>> If one of those columns had a numeric beginning to its name, such as:
>>
>>> colnames(mat)[3] <- "5T"
>>
>> and then using boxplot the same way, it will prepend an "X" to the
>> column name "5T" in the changing to a dataframe.
>>
>> I know I could use boxplot with a formula with the dataframe reshaped
>> which would get round the problem, but I wanted to introduce as few
>> new concepts as possible for someone new to using R.  So the question
>> is: Is there a way to get such a name without anything prepended into
>> boxplot when used this way?
>>
>> I've been led to understand that some Windows' plotting devices lend
>> themselves to simpler editing than in Linux, so perhaps there is a
>> simple way to remove the "X" from the plot afterwards.  I know it
>> could be done with a postscript device by editing the file with a text
>> editor but that's not simple with Windows.

You can edit EMF files in a suitable editor.

>> Ideas?
>
> The name change happens in the conversion to a dataframe, so why not
> change the name afterwards?  That is:
>
> df <- data.frame(mat)
> names(df)[3] <- "5T"
> boxplot(df, main="blah blah blah")

Or use

boxplot(as.data.frame(mat))

which seems more natural than data.frame(mat, check.names=FALSE) (which 
also does the job) or even data.frame(mat).

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