[R] cbind in a loop

Sundar Dorai-Raj sundar.dorai-raj at PDF.COM
Wed Sep 8 23:55:55 CEST 2004



Jan Wantia wrote:

> 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,]))])

You probably want

Min <- cbind(Min, Tabelle[,(which.min(Tabelle[129,]))])

Also, read the posting guide which will tell you the following:

"For new subjects, compose a new message and include the 
'r-help at lists.R-project.org' (or 'r-devel at lists.R-project.org') address 
specifically. (Replying to an existing post and then changing the 
subject messes up the threading in the archives and in many people's 
mail readers.)"

--sundar




More information about the R-help mailing list