How to convert the name of a dataframe to a string in R?

前端 未结 3 1700
余生分开走
余生分开走 2020-12-28 18:28

I am looping over a list of dataframes in R and want to use their names as part of the filename I save my plots under.

The code below is my attempt at iterating thro

相关标签:
3条回答
  • 2020-12-28 18:57

    Slightly different solution:

    dataframe1 = data.frame(iv = rnorm(50), dv = rnorm(50))
    dataframe2 = data.frame(iv = rnorm(50), dv = rnorm(50))
    dataframe3 = data.frame(iv = rnorm(50), dv = rnorm(50))
    
    LIST = list(dataframe1 = dataframe1,
                dataframe2 = dataframe2,
                dataframe3 = dataframe3)
    
    
    for(i in 1:length(LIST)){
      pdf(file=paste(names(LIST)[i], paste(colnames(LIST[[i]]), collapse="."), 
                     "pdf", sep="."))
      plot(LIST[[i]][,1],LIST[[i]][,2], 
           xlab = colnames(LIST[[i]])[1], 
           ylab = colnames(LIST[[i]])[2],
           main = paste("Plot based on data in", names(LIST)[i]))
      dev.off()
      }
    
    0 讨论(0)
  • 2020-12-28 18:59

    The only way I know to work this way directly on the dataframes in a list would be to attach a comment that holds the name, which you can then use to carry its name inside the loop:

    df1 <- data.frame(var1=rnorm(10), var2=rnorm(10))
    df2 <- data.frame(var1=rnorm(10), var2=rnorm(10))
    comment(df1) <- "df1"
    comment(df2) <- "df2"
    
    for ( dataFrame in list(df1,df2) ) { 
         dFnm <- comment(dataFrame) 
         pdf(file=paste( dFnm, "_var1_vs_var2.pdf", sep="" ))
         plot( dataFrame[["var1"]], dataFrame[["var2"]] )     
         dev.off();
    }
    

    (You do lose the names of objects when they get passed as the loop variables. If you do deparse(substitute()) inside that loop, you get "dataFrame" rather than the original names.) The other way would be to use names of the dataframes, but then you will need to use get or do.call, which might get a bit messier. This way seems fairly straightforward.

    0 讨论(0)
  • 2020-12-28 19:01

    The code below answers the question in the title, but may or may not be of any help regarding the question in the body of the post:

    my.data <- read.table(text='
        x1    x2     x3
         1    10    111
         2    20    222
         3    30    333
         4    40    444
         5    50    555
    ', header = TRUE, stringsAsFactors = FALSE)
    
    my.data
    
    deparse(substitute(my.data))
    
    # [1] "my.data"
    

    I found this solution here:

    https://www.mail-archive.com/r-help@r-project.org/msg60789.html

    after fairly extensive searching and thought if might be helpful to others to include the code with the current question, which is the first hit I get when searching the internet for: convert data frame name to string r.

    The answer by BondedDust does mention deparse.

    Here is code that appears to address the question in the body of the post. Although I left out the code for plot generation:

    df1 <- data.frame(var1=rnorm(10), var2=rnorm(10))
    df2 <- data.frame(var1=rnorm(10), var2=rnorm(10))
    
    list.function <-  function() { 
    
         sapply(c("df1", "df2"), get, environment(), simplify = FALSE) 
    } 
    
    my.list <- list.function()
    
    my.df.names <- names(my.list)
    # [1] "df1" "df2"
    
    for (i in 1:length(my.list) ) {
    
         df.name <- my.df.names[i]
         print(df.name)
    
    }
    
    [1] "df1"
    [1] "df2"
    
    0 讨论(0)
提交回复
热议问题