0

I am new to R, so I apologize in advance if this is an elementary question, but I have searched high and low but have not found an answer.

I would like to make a file with more than one data frame within it. (An example is the dataset "flatlizards" in the BradleyTerry2 package. The file is called "flatlizards" but there are two data frames within it--"contests" and "predictors." When you ask to see the data "flatlizards" the two data frames are both shown separately under their corresponding headings).

How do I connect two separately-named data frames into one file? Also, how do I do this with CSV files? Do all the data need to be in one CSV file, or multiple files? Thank you in advance, I really appreciate the help.

Eva
  • 23
  • 4
  • Search the R help page titled "save" (`?save`). – A5C1D2H2I1M1N2O1R2T1 Aug 01 '12 at 06:01
  • 3
    Welcome to Stack Overflow! To the person who silently downvoted: This is the [summer of love](http://blog.stackoverflow.com/2012/07/kicking-off-the-summer-of-love/), so I suggest you do one of a few things 1) Explain why the downvote, 2) Explain to the OP how to improve the question 3) Edit the question so it is a good question. – Andrie Aug 01 '12 at 13:24

3 Answers3

4

The object in question is a list. You can see this by using the following code:

library(BradleyTerry2)
data(flatlizards)
str(flatlizards)

You can see it is a list of 2, and its elements are the separate data frames.

You can combine objects together in a list like this quite easily:

a <- data.frame(x=rnorm(10), y=runif(10))
b <- data.frame(w=rnorm(20), z=runif(20))

ablist <- list(a, b)

From there, you take the approach @mrdwab has taken.

Alternatively, if you want them in one dataframe or one csv, you can try using merge.

sebastian-c
  • 15,057
  • 3
  • 47
  • 93
  • After re-reading the question, I'm not sure if the OP is interested in *saving* as much as they are in creating a `list` of `data.frames`, so +1! – A5C1D2H2I1M1N2O1R2T1 Aug 01 '12 at 07:17
  • Sorry, I think my question was unclear because I didn't know the terminology of it being a "list." Now that sebastian-c has given me some help (thank you!) I can clarify. – Eva Aug 01 '12 at 16:10
3

Here is a very basic example:

# Clean out your current workspace
rm(list=ls())

# Create two objects
a = 1:10
b = letters[1:10]

# Save them to one file
save(a, b, file="demo.RData")

# Remove them from the workspace
rm(a, b)
ls()
## character(0)

# Re-load the data
load("demo.RData")
ls()
## [1] "a" "b"
a
## [1]  1  2  3  4  5  6  7  8  9 10
b
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

The same principles would apply to data in CSV files. Simply read them in, do any cleaning that you need to do, then bundle them into a single .RData file.

There is also save.image() which saves everything in your current workspace.

Update: After reading Sebastian's answer, it seems like you are more interested in creating a list of data.frames. If this is what you're interested in doing, and you need to read the data from separate CSV files, see this post to help you get started.

Community
  • 1
  • 1
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
0

To complete the solution by sebastian-c you can also name the components of a list:

a <- data.frame(x=rnorm(10), y=runif(10))
b <- data.frame(w=rnorm(20), z=runif(20))

ablist <- list(first=a, second=b)

ablist$first
ablist[[1]]

ablist$second
ablist[[2]]
jakob-r
  • 6,824
  • 3
  • 29
  • 47
  • Thank you! This along with sebastian-c's answer is just what I needed! Do I need to redo this every time I start my work? – Eva Aug 01 '12 at 18:08
  • It's hard to know without a context of what you are doing or why you are doing it. If you have dataframes and you need to use a list, you could do that, or you could turn it into a list once and use @mrdwab's solution to just load in the created list. – sebastian-c Aug 02 '12 at 01:28