Splitting up a list of data frames

2,390 views
Skip to first unread message

Elise Hellwig

unread,
Jan 10, 2014, 8:03:09 PM1/10/14
to davi...@googlegroups.com
Hi everyone,

I have what I think is a list of data frames and I want to break the list into its data frames. I am importing a series of files into R using these two lines of code

yieldfiles <- paste0("/accessdata/yield", c("corn.csv", "cc.csv","tomato.csv","wheat.csv"))
yield <- lapply(yieldfiles, read.csv, header=TRUE, sep = ",", stringsAsFactors=FALSE)

When I do this, I get a list of data frames. I use the command

yield[[1]]

or

yield[1]

to access the first data frame in the list.

What I want to do is split this list up into its component data frames. I think I should use split(), but I'm not sure what the f argument would be. Of course if there is a better way to import the data so I don't have to do this that would also be cool. Thanks!

Elise

Noam Ross

unread,
Jan 10, 2014, 8:17:43 PM1/10/14
to Davis R Users Group

If what you want is for your data frames to be present under the global environment, one option is list2env, but for that your list must have names:

names <- ("corn", "cc","tomato","wheat")
yieldfiles <- paste0("/accessdata/yield", names, ".csv")
yield <- lapply(yieldfiles, read.csv, header=TRUE, sep = ",", stringsAsFactors=FALSE)
names(yield) <- names
list2env(yield, environment())

Now you’ll have data frames “corn”, “cc”, etc in your



--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/groups/opt_out.

Vince S. Buffalo

unread,
Jan 10, 2014, 8:58:33 PM1/10/14
to davi...@googlegroups.com
One quick thought — 

You may not want to load each dataframe into your environment. This may be so-called "code smell". If your dataframes have the same columns, it might be better to attach a column indicating the sample name, and then use a do.call/rbind to rbind each dataframe together. This way, your data comes together more easily and this can make downstream analysis much easier.

But this all depends on what you're doing downstream — using list2env may be entirely appropriate for what you're doing.

Hope this helps,
Vince
--
Vince Buffalo
Ross-Ibarra Lab (www.rilab.org)
Plant Sciences, UC Davis

yu pei

unread,
Jan 13, 2014, 11:45:00 AM1/13/14
to davi...@googlegroups.com
If it is only three files, I think you can use for loop, but since three different data.frame might have different rownums or colnums, 
they are in a list is pretty sensible and convenient for further looping. It depends on what you need to do with the data. I think.
Reply all
Reply to author
Forward
0 new messages