[R] "nonstandard" column names

(Ted Harding) Ted.Harding at manchester.ac.uk
Tue Jan 22 00:56:06 CET 2008


On 21-Jan-08 23:36:46, 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.

I'm a bit surprised that x$"CPI/RPI" doesn;t work,
since I just got it to work with:

> D
  A B/C
1 1   2
2 3   4
3 5   6

D$"B/C"
[1] 2 4 6

Are you sure the name really is "CPI/RPI"? In setting
yp my example, I read in a CSV file "temp.csv":

A,B/C
1,2
3,4
5,6

with

D<-read.csv("temp.csv")

and got:

> D
  A B.C
1 1   2
2 3   4
3 5   6

so read.csv() had changed the name from "B/C" to "B.C".
I had to do

names(D)<-c("A","B/C")

to bring it back to the example above.

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 21-Jan-08                                       Time: 23:56:02
------------------------------ XFMail ------------------------------



More information about the R-help mailing list