[R] print dataframe names in loop

Kai Mx govokai at gmail.com
Sat May 16 11:33:24 CEST 2015


Thanks for all the input.
It seems like there is no way around introducing names for the list items,
but the namedList-function is really neat.
Function returns are not at issue for me, I rather want to use it on plots
or outputs in the console.

Best,

Kai


On Fri, May 15, 2015 at 10:51 PM, William Dunlap <wdunlap at tibco.com> wrote:

> You can automate the adding of the names to the list with the following
> function, so you
> can replace the
>   dflist<-list(df1,df2,df3)
>   names(dflist)<-c("df1","df2","df3")
> with
>   dflist <- namedList(df1, df2, df3)
> If you supply names, such in
>   dflist <- namedList(df1, Second=df2, log(df3))
> it will use your names and create names for the unnamed ones.
>
> (Once you make a named list of data.frames, you can remove the the
> originals from your global environment so you will have a single version
> of truth.)
>
> namedList <- function (...)
> {
>     L <- list(...)
>     nms <- names(L)
>     if (is.null(nms)) {
>         nms <- rep("", length(L))
>     }
>     if (any(needsName <- is.na(nms) | !nzchar(nms))) {
>         nms[needsName] <- vapply(substitute(...())[needsName],
>             function(x) deparse(x, nlines = 1L), FUN.VALUE = "")
>         names(L) <- nms
>     }
>     L
> }
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Fri, May 15, 2015 at 5:20 AM, Jim Lemon <drjimlemon at gmail.com> wrote:
>
>> Hi Kai,
>> One way is to name the components of your list with the names of the
>> data frames:
>>
>> df1<-data.frame(a=1:3)
>> df2<-data.frame(a=4:6)
>> df3<-data.frame(a=7:9)
>> dflist<-list(df1,df2,df3)
>> names(dflist)<-c("df1","df2","df3")
>> for(i in 1:length(dflist)) cat(names(dflist)[i],"\n")
>> df1
>> df2
>> df3
>>
>> Jim
>>
>>
>> On Fri, May 15, 2015 at 10:05 PM, Kai Mx <govokai at gmail.com> wrote:
>> > Hi everybody,
>> >
>> > I just can't  figure this out:
>> >
>> > I have a loop trough several dataframes such as
>> >
>> > for (df in list(df1, df2, df3, ...)) {
>> > ..some functions with df..
>> > }
>> >
>> > now I want to print out the current dataframes name (ie the list items
>> > name) with the cat command before the actual functions to have better
>> > orientation in the output.
>> > However, I haven't been successful with different variations of
>> deparse(),
>> > substitute(), (cat(substitute(df)) gives me 'df' for the whole loop).
>> >
>> > Could somebody please enlighten me?
>> >
>> > Thanks so much!
>> >
>> > Best,
>> >
>> > Kai
>> >
>> >         [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > 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.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list