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 at 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]".
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 at 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Henrique Dallazuanna
2010-Mar-11 13:25 UTC
[R] Define column names to a series of data.frames
You can try this: lapply(lapply(ls(pattern = 'DF[0-9]'), get), 'names<-', c("SDev","PC1", "PC2", "PC3", "PC4", "PC5", "PC6")) On Thu, Mar 11, 2010 at 9:33 AM, Nikos Alexandris <nikos.alexandris at felis.uni-freiburg.de> wrote:> 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 at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Gabor Grothendieck
2010-Mar-13 01:40 UTC
[R] Define column names to a series of data.frames
Try this # set up test data # BOD1 and BOD2 are same as built in BOD BOD1 <- BOD2 <- BOD env <- .GlobalEnv object.names <- objects(pattern = "BOD", env) # change col names of BOD1 to A & B. Ditto for BOD2. for(nm in object.names) names(env[[nm]]) <- c("A", "B") On Thu, Mar 11, 2010 at 7:33 AM, Nikos Alexandris <nikos.alexandris at felis.uni-freiburg.de> wrote:> 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 at 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. >