1

I'm facing a problem with R. I created a certain amount of dataframes after a series of subsettings of my big species table, according to sex, collection moment, family etc... I saved them as .csv files in my datasets/csv/ directory, and as you can imagine it gets quite confuse. So I thought to save those dataframes as a list which, after saving it as an r object, I can load as an unique object when I do analysis in a separate RMarkdown file. This will avoid mistakes in loading .csv files and me getting mad to search and load the right file in the /csv directory.

So. After deleting the useless objects I have a situation like this (with more dataframes).

my_list <- replicate(n = 10, expr = {data.frame(x = rnorm(50), y = rnorm(50))}, simplify = F)

names(my_list) <- c(paste("ciao", c(1:10)))
list2env(my_list ,.GlobalEnv)

Obviously the names of the dataframes are not just ciao 1, 2 etc... but are different from each other. Now I would like to "come back" listing and naming each object with its name, something like

my_list2 <- list()
for (i in seq_along(ls())) {
  my_data[["The name of i-th object"]] <- list="i-th object"
}

And here I faced the problem. How can I call just the name of the i-th object using ls() basing on its position, which I get from seq_along? I searched in the help page and in this and other websites, but I found nothing.

Thank you very much. Simone.

Simone Marini
  • 55
  • 1
  • 7
  • Check out my answer to [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames). I think it will be helpful in this situation and may provide a solution. – lmo Mar 15 '17 at 11:31
  • Why are you creating separate objects in the global environment? If they are in a list, leave them there. If you need them in an environment for some unknown reason, put them in a different environment that holds only these objects. – Roland Mar 15 '17 at 11:36
  • @Roland. In my original file there are separate objects. The one in the question was just an example. – Simone Marini Mar 15 '17 at 17:24

1 Answers1

5

Here are some approaches:

1) mget

my_list2 <- mget(ls())

or possibly:

my_list2 <- mget(ls(), .GlobalEnv)

2) eapply

my_list2 <- eapply(.GlobalEnv, identity)

3) get Incrementally expanding a list is not recommended from a performance standpoint but if there are few objects it may not matter. Could use get(nm, .GlobalEnv) isntead of just get(nm):

nms <- ls()
my_list2 <- list()
for(nm in nms) my_list2[[nm]] <- get(nm)

3a) Similar to (3) but uses numeric indexes:

nms <- ls()
my_list2 <- list()
for(i in seq_along(nms)) my_list2[[nms[i]]] <- get(nms[i])

3b) Also uses get but without the for loop:

Map(get, ls())

3c) To specify the environment explicitly:

sapply(ls(), get, .GlobalEnv, simplify = FALSE)

Note: Note that if they come from files you could consider reading them directly into a list using read.table or read.csv:

sapply(list.files(), read.csv, simplify = FALSE)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341