Reputation: 33
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).
Upvotes: 1
Views: 1427
Reputation: 329
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]])
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.
Upvotes: 1
Reputation: 3158
To make the answer generic, since that seems to be what you want, I would make a list
, then populate that list with dataframe
s.
my_list <- list()
for (i in seq(10)) {
my_list[[i]] = data.frame(x=runif(100), y=rnorm(100))
}
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
.)
Upvotes: 3