[R] "nonstandard" column names

Marc Schwartz marc_schwartz at comcast.net
Tue Jan 22 00:55:26 CET 2008


Moshe Olshansky wrote:
> Hi everyone,
> 
> I am sure that this question has been asked here some
> time ago but I do not remember the answer and was
> unable to find it in the archives...
> 
> Below is my question: suppose that I have a data.frame
> x and one of it's columns name is "CPI/RPI" (without
> quotation marks of course). How can I reference this
> column? Neither of x$CPI/RPI or x$"CPI/RPI" work. I
> certainly can do x[,which(colnames(x) == "CPI/RPI")]
> but there should be a nicer way to do this.
> 
> Thank you!
> 
> Moshe Olshansky.

Moshe,

Not a problem here, using 2.6.1 patched:


DF <- data.frame(A = 1:5, B = letters[1:5])
colnames(DF) <- c("CPI/RPI", "Col2")

> DF
  CPI/RPI Col2
1       1    a
2       2    b
3       3    c
4       4    d
5       5    e

> DF$"CPI/RPI"
[1] 1 2 3 4 5

> DF[, "CPI/RPI"]
[1] 1 2 3 4 5

> subset(DF, select = "CPI/RPI")
  CPI/RPI
1       1
2       2
3       3
4       4
5       5


HTH,

Marc Schwartz



More information about the R-help mailing list