Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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 through dataframes, plotting their first column (var1) versus their second (var2) and then saving the plot.

first.data = data.frame( var1 = 1:4, var2 = 5:8 );
second.data = data.frame( var1 = 9:12, var2 = 13:16 );

for ( dataFrame in list(first.data, second.data) ) {
     plot( dataFrame[["var1"]], dataFrame[["var2"]] );
     dev.copy( pdf, paste( dataFrame, "_var1_vs_var2.pdf", sep="" ) );
     dev.off();
}

I expect this loop to produce PDF files with filenames of the form "first.data_var1_vs_var2.pdf" but instead the name of the data frame is replaced with the first column in the frame and so I get something like "c(1, 2, 3, 4)_var1_vs_var2.exchemVbuffer.pdf".

like image 225
holocronweaver Avatar asked May 18 '12 16:05

holocronweaver


Video Answer


3 Answers

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.

like image 181
IRTFM Avatar answered Oct 21 '22 16:10

IRTFM


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/[email protected]/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"
like image 40
Mark Miller Avatar answered Oct 21 '22 15:10

Mark Miller


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()
  }
like image 34
Alex Avatar answered Oct 21 '22 16:10

Alex