3

i've following problem:

I use the for-loop within R to get specific data from a matrix. my code is as follows.

for(i in 1:100){
   T <- as.Date(as.mondate (STARTLISTING)+i)
   DELIST <- (subset(datensatz_Start_End.frame, TIME <= T))[,1]

   write.table(DELIST, file = paste("tab", i, ".csv"), sep="," )
   print(DELIST)
}

Using print, R delivers the data. Using write.table, R delivers the data into different files.

My aim is to aggregate the results from the for-loop within one matrix. (each row for 'i')

But unfortunately I can not make it.


sorry, i'm a real noob within R.

for(i in 1:100)
{
T <- as.Date(as.mondate (STARTLISTING)+i)
DELIST <- (subset(datensatz_Start_End.frame, TIME <= T))[,1]
assign(paste('b',i,sep=''),DELIST)

}

this delivers 100 objects, which contain my results. But what i need is one matrix/dataframe with 100 columns or one list.

Any ideas?


Hey!

Hence I'm not allowed to edit my own answers, here my (simple) solution as follows:

DELIST <- vector("list",100)
for(i in 1:100)

{
T <- as.Date(as.mondate (STARTLISTING)+i)
DELIST[[i]] <- as.character((subset(datensatz_Start_End.frame, TIME <= T))[,1])
}

DELIST[[99]]  ## it is possible to requist the relevant companies for every 'i'

Thx to everyone!

George

elJorge
  • 153
  • 1
  • 2
  • 9
  • Please add reproducible sample for good people here to help you. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – CHP Apr 10 '13 at 14:36

2 Answers2

2

If you want a list you can use lapply instead of loop

LL <- lapply(1:100, 
       function(i) {
         T <- as.Date(as.mondate (STARTLISTING)+i)
         DELIST <- (subset(datensatz_Start_End.frame, TIME <= T))[,1]
         assign(paste('b',i,sep=''),DELIST) 
         }
       )

After that you can rbind results together using do.call

result <- do.call(rbind, LL)

Or if you are confident that columns of all elements of LL are going to be of same, then you can use more efficient rbindlist from package data.table

result <- rbindlist(LL)
CHP
  • 16,981
  • 4
  • 38
  • 57
0

check out rbind function. You can start with empty DELIST.DF and append each row to it inside the loop -

DELIST.DF <- NULL
for(i in 1:100){
   T <- as.Date(as.mondate (STARTLISTING)+i)
   DELIST <- (subset(datensatz_Start_End.frame, TIME <= T))[,1]

   DELIST.DF <- rbind(DELIST.DF, DELIST)

   write.table(DELIST, file = paste("tab", i, ".csv"), sep="," )
   print(DELIST)
}
Nishanth
  • 6,932
  • 5
  • 26
  • 38
  • This is wrong advise. It will quickly lead to [second circle of R hell.](http://www.burns-stat.com/pages/Tutor/R_inferno.pdf) – CHP Apr 10 '13 at 14:48
  • Obviously pre-allocation is better than rbind (you can check my other answers elsewhere). But its not black/white. It is fine to use rbind for 100 iterations. You can check the table in the pdf you linked. – Nishanth Apr 10 '13 at 15:11
  • It will also depend on size of the objects being "rbind"ed. In general it will be pretty inadvisable to grow an object inside a loop. – CHP Apr 10 '13 at 15:16
  • does it? last time I checked, time taken for `malloc` is independent of object size. I agree that in general it is a bad practice, but in particular (< 100 iterations) it is fine. Premature optimization is not advisable. – Nishanth Apr 10 '13 at 15:26
  • I meant the `memory usage` which is of primary concern here. As I understand it, rbind creates a temperory copy of the objects to be bound and hence exponential growth in memory required as no of iteration required. – CHP Apr 10 '13 at 15:37
  • I am really curious now! One temporary object should be sufficient for the entire iteration. Now even if one temporary object is created every iteration, it is linear not exponential. – Nishanth Apr 10 '13 at 15:43
  • according to my understanding the behavior is similar to C++ STL `vector`, which has no temporary objects & exponential growth. It is slow when you grow it because of frequent free-ing & re-allocation. – Nishanth Apr 10 '13 at 15:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27994/discussion-between-geektrader-and-e4e5f4) – CHP Apr 11 '13 at 03:16