5

How would I go about taking elements of a list and making them into dataframes, with each dataframe name consistent with the list element name?

Ex:

exlist <- list(west=c(2,3,4), north=c(2,5,6), east=c(2,4,7))

Where I'm tripping up is in the actual naming of the unique dataframes -- I can't figure out how to do this with a for() loop or with lapply:

for(i in exlist) {
    i <- data.frame(exlist$i)
}

gives me an empty dataframe called i, whereas I'd expect three dataframes to be made (one called west, another called north, and another called east)

When I use lapply syntax and call the individual list element name, I get empty dataframes:

lapply(exlist, function(list) i <- data.frame(list["i"]))

yields

data frame with 0 columns and 0 rows
> $west
  list..i..
1        NA

$north
  list..i..
1        NA

$east
  list..i..
1        NA
Marc Tulla
  • 1,751
  • 2
  • 20
  • 34
  • `lapply(exlist, as.data.frame)` ? – David Arenburg Oct 16 '14 at 22:19
  • @DavidArenburg that will still return a list. I think @MarcTulla needs to have `assign(i, data.frame(exlist$i)` inside the loop. – ilir Oct 16 '14 at 22:24
  • @ilir, Are you sure he wants them in the global environment? He only said he wants to convert them to data.frames. If you are right, he should go with: `list2env(lapply(exlist, as.data.frame), .GlobalEnv)` – David Arenburg Oct 16 '14 at 22:25
  • @DavidArenburg that's what I gathered. Maybe I'm wrong. `list2env` seems like a nicer solution than what I just proposed. – ilir Oct 16 '14 at 22:28

2 Answers2

4

If you want to convert your list elements to data.frames, you can try either

lapply(exlist, as.data.frame)

Or (as suggested by @Richard), depends on your desired output:

lapply(exlist, as.data.frame.list)

It is always recommended to keep multiple data frames in a list rather than polluting your global environment, but if you insist on doing this, you could use list2env (don't do this), such as:

list2env(lapply(exlist, as.data.frame.list), .GlobalEnv)
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • 1
    It's interesting how `as.data.frame.list` applies the column names. I never noticed that it takes the vector values into account – Rich Scriven Oct 16 '14 at 22:35
  • @RichardScriven, yes, I actually learned a new thing from your comment too :) – David Arenburg Oct 16 '14 at 22:36
  • I think it probably does something like `x <- c(2,4,7); make.names(x)` – Rich Scriven Oct 16 '14 at 22:38
  • @ David, the list2env solution is actually what I'm looking for, but why do you say this is not recommended? Basically I'm looking for a way to read in a bunch of datasets without needing a "read.csv" command for each line, but each of those datasets need to be operated on individually (some need number formatting changes, some need column name changes, etc.) – Marc Tulla Oct 16 '14 at 23:16
  • See [this discussion in comments on my answer](http://stackoverflow.com/questions/25761656/assign-data-frame-name-to-list-elements-using-name-vector/25761850#25761850) by the creator of `list2env` himself reccomnding to avoid it – David Arenburg Oct 16 '14 at 23:20
0

This should create the three objects you want:

df.names <- "value"   ## vector with column names here
for (i in names(exlist)) setNames(assign(i, data.frame(exlist[[i]])), df.names)
ilir
  • 3,236
  • 15
  • 23