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

Petr PIKAL petr.pikal at precheza.cz
Thu Mar 11 14:16:21 CET 2010


Hi

This seems to be nice example why lists shall be used and not several data 
frames scattered in environment.

put your data frames to list
lapply(list.of.data, function(x) names(x) <- column_names)

Regards
Petr



r-help-bounces at r-project.org napsal dne 11.03.2010 13:33:33:

> Greets to the list!
> 
> I am aware that this topic has been discussed several times. And I've
> read quite some related posts [1]. Yet, can't seem to give a solution to
> my problem.
> 
> I have 6 data frames consisting of 6 rows x 7 columns put together from
> other data.frames.
> 
> Something like:
> 
>    a b c d e f g
> v1 # # # # # # # 
> v2 # # # # # # #
> v3 # # # # # # #
> v4 # # # # # # #
> v5 # # # # # # #
> v6 # # # # # # #
> 
> 
> I want to give the following column names to each data.frame: ("SDev",
> "PC1", "PC2", "PC3", "PC4", "PC5", "PC6")
> 
> Works fine for one data.frame:
> column_names <- c("SDev", "PC1", "PC2", "PC3", "PC4", "PC5", "PC6")
> names( df1 ) <- column_names
> 
> How is it to be done at once for all data.frames that the function
> objects(pattern = "SomePattern") can find?
> 
> I've tried several things with assign, get, paste, for but I am not
> getting anywhere. I need to integrate this in a function that can handle
> lot's of data.frames and not only 6.
> 
> Thank you, Nikos
> 
> 
> ---
> [1] I am stuck on this post:
> <http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg32063.html>. 
> 
> It reads:
> 
> dframes <- c("a","b","c")
> cols <- c("one","two")
> df <- data.frame(11:20, 21:30)
> names(df) <- cols
> assign(dframes[2], df)
> 
> Can't understand the logic behind the "[2]".
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list