[R] cbind in a loop

Jan Wantia wantia at ifi.unizh.ch
Wed Sep 8 23:48:07 CEST 2004


Dear all,

I have a problem with adding columns to a data structure, using 'cbind':

I create an array, to which I want to cbind one new colum for every 
iteration of a loop.  Without the loop, the code works and looks like this:

 > Min<- array(0,c(129,0))
 > Min

  [1,]
  [2,]
  [3,]
  [4,]
  etc..

 >  file <- paste("Path\\to\\file\\Run_1", sep="")
 >  Tabelle <- read.table(file, sep=";", skip = 8)
 >  cbind(Min, Tabelle[,(which.min(Tabelle[129,]))])
        [,1]
  [1,] 25.45
  [2,] 25.33
  [3,] 25.76
  [4,] 25.40
  [5,] 24.39
  etc.

However, with the loop the result is the following:

 > for(i in 1:2){
+ file <- paste("Path\\to\\file\\Run_",i, sep="")
+ Tabelle <- read.table(file, sep=";", skip = 8)
+ cbind(Min, Tabelle[,(which.min(Tabelle[129,]))])
+ }
 > Min

  [1,]
  [2,]
  [3,]
  [4,]
  [5,]
  [6,]
...
I suppose the solution is very easy, but I just do not find it.
(I am using R 1.9.1 on windows XP)

Thanks in advance!
Jan




More information about the R-help mailing list