[R] Create vector of data frames
Duncan Murdoch
murdoch at stats.uwo.ca
Wed Nov 19 17:33:54 CET 2008
On 11/19/2008 11:27 AM, Brigid Mooney wrote:
> Hi All,
>
> I'm sorry I haven't been able to find anything that will help me with this
> problem, and I'm still pretty new to R - so any help here is greatly
> appreciated!
>
> I am looking to create a vector in a sequential process where each entry in
> the vector is a data frame, for example:
>
>
> days <- 3
> for (i in 1:days)
> {
> FOO[i] <- data.frame(x=c(i, i+1, i+2), y=c(i, i*i, i*i*i))
> }
>
> but when I try this, I get the error "object "FOO" not found".
>
> I tried to avoid this by concatenating blank data frames to create a shell
> for FOO via:
>
> FOO <- rep(data.frame(), times=days)
You want to create FOO as a "list", which is the generic vector type in
R. That is:
FOO <- list()
days <- 3
for (i in 1:days)
{
FOO[[i]] <- data.frame(x=c(i, i+1, i+2), y=c(i, i*i, i*i*i))
}
Note the use of [[]] indexing on FOO: FOO[i] would be a list shortened
to just one element; FOO[[i]] is that element. (This distinction only
matters when working with types like lists. There's no such thing as a
numeric value that's not in a numeric vector, so x[[2]] and x[2] are the
same thing if x is numeric.)
Duncan Murdoch
>
> before the other lines, but then I get lots of errors relating to replacing
> 0 rows with 3 rows.
>
>
> Needless to say, the data frames I am actually dealing with are quite a bit
> larger than listed here - so I wanted to get the process working on a toy
> example first.
>
> Thanks in advance for your help!
>
>
>
>
>
>
>
> for (i in 1:3)
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list