0

I have a number of data.frames, with names apple, banana, and coffee. I want to create, and then export, new dataframes in a for-loop corresponding to each one, call them apple_new, banana_new, and coffee_new.

I have in mind something along the lines of this pseudo-code

for (x in c("apple", "banana", "coffee")) { newdf_name <- paste0(x, "_new") #insert some magical function here that: # (1) creates a new data frame as a copy of the dataframe {x} # (2) calls that new dataframe {x}_new, # i.e., the string contents of newdf_name # become the name of a new variable write.csv(x_new) } 

The basic problem with the above is that at different points in this loop, I want x to refer to either a string "apple" or the data frame named apple (and analogously, I want to create a data frame named apple_new such that the starting point for that data frame is apple_new <- apple.

I'm assuming there must be a way for R to take a string input, tell R that it refers to a variable name, and then perform operations on that variable.

The closest thing I've found is to create a list of named list of data frames so that that I can refer to either the string or the variable as needed. That is:

newlist <- list(apple = apple, banana = banana, coffee = coffee) for (i in 1:length(newlist)) { #create new dataframe based on the original df <- newlist[[i]] #export: outfile <- paste0(names(newlist[i]),"_new.csv") write.csv(df, outfile) } 

This solution is sub-optimal because (a) I have to hard-code the new list of names/corresponding data frames, and (b) it doesn't actually create the desired new data frames apple_new, banana_new, etc.

New contributor
s84492025 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
17
  • 1
    There are plenty of options (which are more R-like). Currently it appears like you want to rename files (on disk) and collect them as data.frames in a list (in your IDE of choice). Am I correct? Commented 2 days ago
  • 1
    Please edit your question to provide a minimal reproducible example of your code and data along with relevant errors so that readers can run your code to answer your question. You might also want to ask a single question, and ask your question in separate posts (wait to get one answered, then ask about the next step). Commented 2 days ago
  • @Friede I'm not merely trying to rename files on the disk, I'm doing data processing in the middle that's not pertinent to the question so I eliminated it from the code block. I'm basically trying to see if there's a way to make R take in, let's say, a string, in a for-loop, and then interpret that string as the name of a variable. Commented 2 days ago
  • @M I'm not sure how to provide reproducible code without somehow attaching SAS input data. Also, though I expressed the question in multiple parts, it's really leading up to a single question that I think requires more context to properly understand. I'm not sure who was responsible for closing the question but I guess I will try to simplify it to better get at the heart of what I'm trying to understand. Commented 2 days ago
  • You don't need SAS input data as the details of the reading and writing are inessential to the substance of the question. Per @M--'s comment I've split my answer up a bit to separate the dummy example from the solution. You could take or adapt the toy read and write functions as a basis for a reprex. Commented 2 days ago

1 Answer 1

1

An outline might be:

# replace all this dummy functionality with your actual process setup # (which we can't reproduce) silly_read <- function(nm) data.frame(col=paste("I'm from", nm)) silly_write <- function(x, nm) write.csv(x, file=paste0(nm,".csv")) process <- function(nm) { df = silly_read(nm) # read/convert to data.frame cat(nm, "has been read\n") silly_write(df, nm) # write back out cat(nm, "has been written\n") df # return the data.frame } 
# then, given the function `process()`, a solution is this: nms <- c("a", "b", "c") # names of the files (a vector - you don't need a list) # process each file name in a loop, storing each returned object in a list lst <- lapply(nms, process) # name the items in the list names(lst) <- nms # access like this lst$a 
> lst$a col 1 I'm from a 
Sign up to request clarification or add additional context in comments.

21 Comments

We want to almost always avoid this: list2env(lst, envir = .GlobalEnv). Also, I do not see where OP asked for this.
I did have a queasy feeling writing that line (!) but was going off "The problem with this solution is that it doesn't keep the intermediate dataframes in memory, and I actually do want to have those for later use". But if that "later use" is just some further processing and re-exporting of each individual dataset, then they never need to be assigned.
If it really were necessary to retain them in memory, what would be the best way - just leave them in the list? Or there other patterns? I didn't want to overcomplicate the answer.
It depends. Typically either in a list or merged/row binded into a single data.frame (with optional id-column) if possible.
In any case I think you're quite right in your reading of the question - it really is just about generalising a workflow over several files while making use of names or name patterns. On that basis I'll pull this answer in a bit but I'll leave it up for now in case it helps OP create a reprex.
pastebin.com/6C6emBsj It's in line with guidelines and concept to downvote silently. Sometimes I understand why but most often I do not. Now you are back to "0".
Ah, okay. And in that case sorry to the person concerned (and thank you to you for the compensating vote.) That said, why don't I just pull this answer and let you provide one. I think a good answer would point out that 1) if the files can be processed individually then who cares what name (symbol) is assigned to the imported object on each loop (it can be df), 2) if objects need to be held in memory simultaneously then read them into a named list (and leave them there!), 3) here are some useful tricks for automated naming according to patterns that alleviate the need for manual nami
@s84492025 '"I have a string and I want to tell R to interpret it as the name of a variable," ... But I'd still like to know if it's something that can be done!' Yes, that can be done. It's not even difficult. However, it leads to really bad code and serious problems. If you have to ask this question, nobody should show you how to do that.
@RuiBarrads why making it worse with loading a function from external package which isn't needed and using a multivariate loop where an univariate is enough?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.