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

Duncan Murdoch murdoch at stats.uwo.ca
Mon Apr 16 22:40:38 CEST 2007


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.
> 
> 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")

Duncan Murdoch



More information about the R-help mailing list