[R] "nonstandard" column names

Marc Schwartz marc_schwartz at comcast.net
Tue Jan 22 01:13:56 CET 2008


<snip>

(Ted Harding) wrote:
> 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.


Ted,

The read.table() family has an argument 'check.names' which is TRUE by
default. This calls make.names() to check the syntactical validity of
the column names:

> make.names("B/C")
[1] "B.C"

See ?make.names for more detail.

If you had used:

  D <- read.csv("temp.csv", check.names = FALSE)

the column names would have been unaltered.

For example, reading your data from the clipboard:

> read.csv("clipboard")
  A B.C
1 1   2
2 3   4
3 5   6

> read.csv("clipboard", check.names = FALSE)
  A B/C
1 1   2
2 3   4
3 5   6

Regards,

Marc



More information about the R-help mailing list