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

Linlin Yan yanlinlin82 at gmail.com
Thu Mar 11 14:39:37 CET 2010


It seems that the names of original data frames have not changed in
this way. I guess textConnection() could help, like this:

for (name in objects(pattern = "df[0-9]"))
eval(parse(textConnection(paste('names(', name, ') <-
column_names'))))

On Thu, Mar 11, 2010 at 9:25 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> 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@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
>
> ______________________________________________
> 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