[R] Define column names to a series of data.frames

Nikos Alexandris nikos.alexandris at felis.uni-freiburg.de
Sat Mar 20 03:38:33 CET 2010


Nikos Alexandris wrote:
... 
> >>> I have 6 data frames consisting of 6 rows x 7 columns put together from
> >>> other data.frames.
... 
> >>> I want to give the following column names to each data.frame: ("SDev",
> >>> "PC1", "PC2", "PC3", "PC4", "PC5", "PC6")
... 
> >>> How is it to be done at once for all data.frames that the function
> >>> objects(pattern = "SomePattern") can find?

Henrique Dallazuanna wrote:
> Yes, just in the list.
> If they want change the name in Environment GlobalEnv:
> 
>  for(i in ls(pattern = "DF[0-9]"))
>        assign(i, `names<-`(get(i), c("SDev","PC1", "PC2", "PC3",
> "PC4", "PC5", "PC6")), globalenv())

It works also using colnames() and rownames(), for example:

--%<---
# collect results based on svd
svd.results <- objects ( pattern = "^svd.result_of_.*" )

# collect results based on eigenvector(s)
eigenvector.results <- objects ( pattern = "^eigenvector.result_of_.*" )

# combine all in one
pca.results <- c ( svd.results , eigenvector.results )

# (re-)define column names
for ( i in pca.results ) assign ( i , `colnames<-` ( get ( i ) ,
column_names ) , globalenv ( ) )

# (re-)define row names
for ( i in pca.results ) assign ( i , `rownames<-` ( get ( i ) ,
row_names ) , globalenv ( ) )
--%<---

Henrique D., you are a guRu!
Thank you, Nikos



More information about the R-help mailing list