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

前端 未结 3 1718
余生分开走
余生分开走 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()
      }
    

提交回复
热议问题