Creating data frames in a loop

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating data frames in a loop

Nish
Hello,

I am new to R and have a question on creating data frames at run time in a loop. How can I create it? For example, something like the following which will create 20 dataframes.

for(i in 1:20) {
 ddat_<current value of i> <- data
 other processing...
}
 
Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Creating data frames in a loop

David Winsemius

On May 11, 2010, at 11:01 PM, Nish wrote:

>
> Hello,
>
> I am new to R and have a question on creating data frames at run  
> time in a
> loop. How can I create it? For example, something like the following  
> which
> will create 20 dataframes.

Well not unless you
>
> for(i in 1:20) {
> ddat_<current value of i> <- data
> other processing...
> }
>

ddat <- as.list(rep("", 20))
for(i in 1:20) {
         ddat[[i]] <- data.frame(ivec = 1:i)
         #other processing..
                 }

--
David.

> Thanks.
> --
> View this message in context: http://r.789695.n4.nabble.com/Creating-data-frames-in-a-loop-tp2195527p2195527.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Creating data frames in a loop

sayan dasgupta
Well you can try
for(i in 1:20) {
  assign(paste("ddat",i,sep="_"),data)
other processing...
}

Reply | Threaded
Open this post in threaded view
|

Re: Creating data frames in a loop

Nish
Thank yoy. this works.