[datatable-help] Simpe script does not finish

jim holtman jholtman at gmail.com
Wed Jan 6 14:21:24 CET 2016


​It is bad practice to keep growing objects like 'd' in the loop.  Here is
a modified version that runs in under 1 second.

> > # Read the text file (512x512)
>  df<-matrix(runif(512 * 512), 512)
> my.stats('start')
start (1) - Rgui : 08:19:36 <282.7 415537.0> 415537.0 : 108.9MB
>  d=matrix(NA,512 * 512, 3)  # create output matrix
>  for (i in 0:511){
+    for (j in 0:511){
+      d[i * 512 + j + 1,] <- c(i,j,df[i+1,j+1])
+      # Myrow<-c(i,j,df[i+1,j+1])
+      # d=rbind(d,Myrow)
+    }
+  }
> my.stats('done')
done (1) - Rgui : 08:19:37 <283.9 415538.1> 415538.1 : 164.9MB
> head(d, 10)
      [,1] [,2]       [,3]
 [1,]    0    0 0.72414660
 [2,]    0    1 0.26716396
 [3,]    0    2 0.11328281
 [4,]    0    3 0.05107802
 [5,]    0    4 0.44158025
 [6,]    0    5 0.09608051
 [7,]    0    6 0.18925725
 [8,]    0    7 0.64978998
 [9,]    0    8 0.51816944
[10,]    0    9 0.83320742

​
​


Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Tue, Jan 5, 2016 at 7:06 AM, hello_R <shariful at excite.com> wrote:

> Hi all,
> I have a dataframe (512x512). I just want to create a new dataframe from
> it.
> I used the following code. It does not stop running! Could you please help.
> Thanks
>
> # Read the text file (512x512)
> df<-read.table("out_1_1.txt")
>
> d=NULL
> for (i in 0:511){
>   for (j in 0:511){
>     Myrow<-c(i,j,df[i+1,j+1])
>     d=rbind(d,Myrow)
>   }
> }
>
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Simpe-script-does-not-finish-tp4716161.html
> Sent from the datatable-help mailing list archive at Nabble.com.
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20160106/aa1681cf/attachment.html>


More information about the datatable-help mailing list