I have a matrix with numeric column labels, for example:
1,2,3
1,2,3
4,5,6
7,8,9
When I load this data into a variable in R using as.matrix(read.table("myfile", sep=",", header=TRUE) and print the resulting matrix, the column headers have been prepended with X, and this extra character appears in plots and the like:
X1 X2 X3
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
How can I stop this behaviour?
Supply
check.names=FALSEtoread.table.Be aware that when
data.frames have non-standard names such as this any reference to their columns must be surrounded with tick marks, e.g.Also, calls to
transform, such astransform(my.df,1=1:3), will replace the column names with their standardized forms (X1,X2,X3) becausetransformcallsdata.frame()with its default arguments.Setting
check.names=FALSE, while useful in some very particular situations, will allow duplicate column names and non syntactic names, which could cause any number of problems later on. Be cautious when using this.data.frame()usescheck.names=TRUEby default because in many contexts R tries to interpret the column names of a data frame as variables in their own right; if the column names are not syntactically valid names (see?make.names), or protected with back-ticks as described above, then errors will occur in these contexts.