25

I have been searching high and low for what I think is an easy solution.

I have a large data frame that I split by factors.

eqRegions <- split(eqDataAll, eqDataAll$SeismicRegion)

This now creates a list object of the data frames by region; there are 8 in total. I would like to loop through the list to make individual data frames using another name.

I can execute the following to convert the list items to individual data frames, but I am thinking that there is a loop mechanism that is fast if I have many factors.

testRegion1 <- eqRegions[[1]]

testRegion3 <- eqRegions[[3]]

I can manually perform the above and it handles it nicely, but if I have many regions it's not efficient. What I would like to do is the equivalent of the following:

for (i in 1:length(eqRegions)) {
   region[i] <- as.data.frame(eqRegions[[i]])
}

I think the key is to define region before the loop, but it keep overwriting itself and not incrementing. Many thanks.

Frank
  • 66,179
  • 8
  • 96
  • 180
Bryan Butler
  • 1,750
  • 1
  • 19
  • 19

5 Answers5

51

Try

list2env(eqRegions,envir=.GlobalEnv)
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
Marat Talipov
  • 13,064
  • 5
  • 34
  • 53
6

This should work. The name of the data.frames created will be equal to the names within eqDataAll$SeismicRegion. Anyways, this practice of populating individual data.frames is not recommended. The more I work with R, the more I love/use list.

lapply(names(eqRegions), function(x) assign(x, eqRegions[[x]], envir = .GlobalEnv))

edit: Use list2env solution posted. Was not aware of list2env function.

Vlo
  • 3,168
  • 13
  • 27
3

attach(eqRegions) should be enough. But I recommend working with them in list form using lapply. I guarantee it will result in simpler code.

Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
  • This puts the list on your search path, so the variables will not show up with `ls`, but in the example in your comment, you can get `8` with ` ```8``` ` . – Matthew Plourde May 28 '15 at 21:01
  • Hi Matthew, sorry i had deleted comment, reposted: I cant quite see how this works: eg `s <- split(mtcars, mtcars$cyl); attach(s)`. Using `ls()` doesnt show up, and using `'8'` doesnt give the final table (it does work if i had labelled the cyl levels lettters[1:3] – user20650 May 28 '15 at 23:33
  • 1
    To reference variables with non-standard names, like those starting with a number, surround the name in backticks instead of quotes. – Matthew Plourde May 29 '15 at 12:16
3

list2env returns data frames to the global environment whose names are the names in the list. An alternative, if you want to have the same name for the data frames but identified by i from a loop:

for (i in 1:length(eqRegions)) {
  assign(paste0("eqRegions", i), as.data.frame(eqRegions[[i]]))
}

This can be slow if the length if the list gets too long.

PrashanthVajjhala
  • 91
  • 1
  • 2
  • 11
2

As an alternative, a "best practice" when splitting data like this is to keep the data.frames within a list, as provided by split. To process it, you use either one of sapply or lapply (many factors) and capture the output back in a list. For instance:

eqRegionsProcessed <- lapply(eqRegions, function(df) {
    ## do something meaningful here
})

This obviously only works if you are doing the same thing to each data.frame.

If you really must break them out and deal with each data.frame uniquely, then @MatthewPlourde's and @MaratTalipov's answers will work.

r2evans
  • 141,215
  • 6
  • 77
  • 149