Dear R-help, I am trying to rename the variables in a dataframe, called 'T1A' here. Seems renaming was successful, but when I call one of the variable I got error message and I wanted to know why. The data frame contains 365 rows and 49 columns. I would like to name the first column `DATE` and the others T0.5, T1, T1.5,...,T24 (as this is a set of data collected every half hour for a whole year). Original data is saved as csv file and column 2-49 are named in format '00:30,01:00,01:30,...,23:30,00:00'. When I read them into R by using read.csv, the column names are changed automatically as 'X0.30.00, X1.00.00,...,X23.30.00,X0.00.00' , which dont look great (i mean I would prefer it in a format as 'hh:mm', NOT using 'dot' between numbers that used to indicate time, but I have not found a solution...). So I decided to use a simplified version as above, e.g. T0.5, T1, T1.5,...,T24 and my code is: TIME<-paste(rep("T",48),as.character(seq(0.5,24,by=0.5))) names(T1A)<-c("DATE",TIME)> class(T1A$T0.5) ## without a space between 'T' and '0.5'[1] "NULL"> class(T1A$T 0.5) ## with a space between 'T' and '0.5'Error: unexpected numeric constant in "class(T1A$T 0.5" I also tried the code below, but got same error message... TIME<-paste(rep("T",48),seq(0.5,24,by=0.5)) names(T1A)<-c("DATE",TIME) However, if I do not change the columns' name then everything works fine, e.g. I can call the variables with no problem. class(T1A$X00.30.00) [1] "numeric" Any thoughts?? Many thanks!!! HJ [[alternative HTML version deleted]]
On 19-03-2012, at 17:39, HJ YAN wrote:> Dear R-help, > > I am trying to rename the variables in a dataframe, called 'T1A' here. > Seems renaming was successful, but when I call one of the variable I got > error message and I wanted to know why. > > > The data frame contains 365 rows and 49 columns. I would like to name the > first column `DATE` and the others T0.5, T1, T1.5,...,T24 (as this is a set > of data collected every half hour for a whole year). > > Original data is saved as csv file and column 2-49 are named in format > '00:30,01:00,01:30,...,23:30,00:00'. When I read them into R by using > read.csv, the column names are changed automatically as 'X0.30.00, > X1.00.00,...,X23.30.00,X0.00.00' , which dont look great (i mean I would > prefer it in a format as 'hh:mm', NOT using 'dot' between numbers that used > to indicate time, but I have not found a solution...). So I decided to use > a simplified version as above, e.g. T0.5, T1, T1.5,...,T24 and my code is: > > > TIME<-paste(rep("T",48),as.character(seq(0.5,24,by=0.5))) > names(T1A)<-c("DATE",TIME) > >> class(T1A$T0.5) ## without a space between 'T' and '0.5' > [1] "NULL" >> class(T1A$T 0.5) ## with a space between 'T' and '0.5' > Error: unexpected numeric constant in "class(T1A$T 0.5" > > > I also tried the code below, but got same error message... > > TIME<-paste(rep("T",48),seq(0.5,24,by=0.5)) > names(T1A)<-c("DATE",TIME) > > > However, if I do not change the columns' name then everything works > fine, e.g. I can call the variables with no problem. > > class(T1A$X00.30.00) > [1] "numeric" > > Any thoughts?? >Have you done ?paste The default separator character is a singe space. Use paste(....., sep="") Berend
On 2012-03-19 09:39, HJ YAN wrote:> Dear R-help, > > I am trying to rename the variables in a dataframe, called 'T1A' here. > Seems renaming was successful, but when I call one of the variable I got > error message and I wanted to know why. > > > The data frame contains 365 rows and 49 columns. I would like to name the > first column `DATE` and the others T0.5, T1, T1.5,...,T24 (as this is a set > of data collected every half hour for a whole year). > > Original data is saved as csv file and column 2-49 are named in format > '00:30,01:00,01:30,...,23:30,00:00'. When I read them into R by using > read.csv, the column names are changed automatically as 'X0.30.00, > X1.00.00,...,X23.30.00,X0.00.00' , which dont look great (i mean I would > prefer it in a format as 'hh:mm', NOT using 'dot' between numbers that used > to indicate time, but I have not found a solution...). So I decided to use > a simplified version as above, e.g. T0.5, T1, T1.5,...,T24 and my code is: > > > TIME<-paste(rep("T",48),as.character(seq(0.5,24,by=0.5))) > names(T1A)<-c("DATE",TIME) > >> class(T1A$T0.5) ## without a space between 'T' and '0.5' > [1] "NULL" >> class(T1A$T 0.5) ## with a space between 'T' and '0.5' > Error: unexpected numeric constant in "class(T1A$T 0.5" > > > I also tried the code below, but got same error message... > > TIME<-paste(rep("T",48),seq(0.5,24,by=0.5)) > names(T1A)<-c("DATE",TIME) > > > However, if I do not change the columns' name then everything works > fine, e.g. I can call the variables with no problem. > > class(T1A$X00.30.00) > [1] "numeric" > > Any thoughts?? > > > Many thanks!!! > HJBerend has shown you the problem with your use of paste(). If you want the original (illegal in R) names, then you can set the argument 'check.names' to FALSE in your read.csv() call. You will then have to remember to always put quotes around any use of these names in your code. But since it's generally better to use T1A[["name"]] rather than T1A$name anyway, the need for quotes should not be a problem. Still, I wouldn't use illegal names. Peter Ehlers