1

If I have a list of dfs in my environment, and I would like to rename them base on a mapping file I created, how can I do that?

my mapping file is looks like this:

mapping<- structure(list(Tab = c("Study", "Score", "Level", "Level2", 
"Level3", "level_Grouped"), DatasetName = c("Output3", "Output4", 
"Output5", "Output6", "Output7", "Output3.g")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

I am thinking to get the output in the list first, then loop through the rename process. but I kind of lost in the process. what should I use for rename a df? Could anyone guide me on this?

output_list<-ls(pattern="^Output")
lapply(output_list, function(x)...)

Thanks.

Stataq
  • 2,237
  • 6
  • 14
  • 1
    Almost always, the answer to this is "if you have a list of data frames, then you should put them in an actual list object". This stops you having to do ugly and dangerous things with `get()` and `assign()`. You just do `names(my_list_of_dfs) <- ...` and rename them however you want. Of course this may not be possible in your case. If so, `get()` and `assign()` are indeed your friends. – dash2 Nov 07 '21 at 18:21

1 Answers1

1

I don't think this is possible because you cannot rename data.frame in R. More info here: How can I change the name of a data frame

Dave4048
  • 173
  • 10
  • is it a way that I can reach the goal of `mapping$Tab` <- `mapping$DatasetName` without manually write it one by one? – Stataq Nov 07 '21 at 18:14
  • Maybe with a workaround but I'm not sure. I tried some things but didn't manage to do what you want – Dave4048 Nov 07 '21 at 18:41