I wrote this simple function to rename columns in a data frame to standardized names in R. This function is useful when we have a very large data set with large number of columns especially for machine learning applications.
colRename<-function(x){
for(i in 1:ncol(x)){
colnames(x)[i] <- paste("column",i,sep="")
}
return(x)
}
An example
library(quantmod)
colRename<-function(x){
for(i in 1:ncol(x)){
colnames(x)[i] <- paste("column",i,sep="")
}
return(x)
}
aapl=getSymbols("AAPL",from="2015-01-01",auto.assign=F)
head(colRename(aapl))
Result
column1 column2 column3 column4 column5 column6
2015-01-02 111.39 111.44 107.35 109.33 53204600 105.6986
2015-01-05 108.29 108.65 105.41 106.25 64285500 102.7209
2015-01-06 106.54 107.43 104.63 106.26 65797100 102.7306
2015-01-07 107.20 108.20 106.70 107.75 40105900 104.1711
2015-01-08 109.23 112.15 108.70 111.89 59364500 108.1736
2015-01-09 112.67 113.25 110.21 112.01 53699500 108.2896
names(x) <- paste0("column", seq_len(ncol(x)))
which will only modify the names attributes of your object and not reassign the data in memory. \$\endgroup\$,
and around=
as well as<-
. Improves readability. More if interested: google.github.io/styleguide/Rguide.xml \$\endgroup\$