1

I am very new to r and am working on developing code to simulate a set of equations for my job.

I want to create multiple empty data frames based on an input variable. That is, if n=4, I want to create 4 separate data frames with separate names such as x1, x2, x3, x4. If n=10, i want 10 data frames, etc.

I want to be able to see these data frames in the global environment (that open up looking similar to an excel sheet).

  • I provided an answer below. It does not address your "looking similar to an excel sheet" issue, but I'm not really sure what you meant by that. I'm assuming that since you're new, you might simply be saying, "I want to be able to look at them". I did answer that part. – Mike Williamson Jul 29 '17 at 18:15
  • You would benefit from reading and trying the code to gregor's answer to [this post](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames). As discussed below, you probably want a list of data.frames. constructing these prior to an analysis is usually not the right way to go. – lmo Jul 29 '17 at 20:47

2 Answers2

3

Code

To make the answer generic, since that seems to be what you want, I would make a list, then populate that list with dataframes.

my_list <- list()
for (i in seq(10)) {
    my_list[[i]] = data.frame(x=runif(100), y=rnorm(100))
}

Explanation

Upon execution of this code, you will have a list with 10 items, labelled 1 - 10. Each of those items is its own dataframe, with 2 columns: one containing 100 uniform random numbers, and another containing 100 Gaussian random numbers (chosen from a standard normal distribution).

If you want to access, say, the third dataframe in the list, you'd simply type

my_list[[3]]

to get the contents of that dataframe.

(Lists use the double bracket notation in R, and you just have to "get used to it". It's fairly easy to figure out how to use them properly, though. E.g., my_list[3] will return a list with only 1 item in it, which is that third dataframe. But my_list[[3]] - notice the extra bracket - will return a dataframe, the third dataframe.)

Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
  • You forgot the df's names part. `names(my_list) <- paste0("x", seq_along(my_list))`. – Rui Barradas Jul 29 '17 at 18:35
  • Thank you! This should work. Here is an example of how I plan to use this code: `n=6 Rate <- list() for (i in seq(n)) { Rate[[i]] = data.frame(session = c(1:30), target = 0, alternative = 0) }` Using `Rate[[3]]` the dataframe shows up in the console. But ive used dataframes before like this: `SRrate <- data.frame(session = c(1:20), target = 0, alternative = 0)` producing a dataframe that looks like [this](http://imgur.com/TyOddfl) Is there a way to create n number of dataframes like my example, w/ separate names like "Rate1" etc – Kaitlyn Browning Jul 29 '17 at 21:09
  • @RuiBarradas There's no require to name the DFs in the list. But sure, if you want to, you can do that. – Mike Williamson Jul 30 '17 at 00:30
  • @KaitlynBrowning Yes, you're already receiving dataframes that look like what you've provided in the link. However, lists do not display **within RStudio** the same as dataframes. If you wish to see that dataframe, then just name a global variable with that dataframe's name. E.g., just say `tmp = my_list[[3]]`. Then you can look at `tmp` the same way as the link you showed. But, you don't want to get into the habit of ["polluting the global environment"](https://stackoverflow.com/questions/13081696/how-to-prevent-functions-polluting-global-namespace), so best to keep as `my_list`. – Mike Williamson Jul 30 '17 at 00:34
  • 1
    @KaitlynBrowning BTW, since you're new: if this is helpful, then either vote up the answer (you like it) or accept the answer (it worked for you), or both. That encourages folks like me to help folks like you. – Mike Williamson Jul 30 '17 at 00:35
1

Use R Studio to run R and to get an Excel-spreadsheety look at your data:

View (name.of.your.list [[n]])

where name.of.your.list is the name of your list of data.frames, and n is the n'th data.frame you want to view.

If you will have a list of lists of data.frames, then just keep tagging [[n's]]

View (name.of.your.list [[n]][[n2]])

As an example:

dat.all = list ()

dat.all [[1]] = list ()

dat.all [[1]][[1]] = data.table ("lol" = 1:5, "whatever" = 6:10)

View (dat.all [[1]][[1]])

Excel-like table opens :O

Also, if you are new to R like me, then I suggest learning data.table instead of data.frame, it is much more powerful, and will probably prevent you from having to make lists of lists of data.frames.

Cheers.

JVP
  • 309
  • 1
  • 11