[R] What is wrong with this FOR-loop?

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Mon Dec 5 13:05:28 CET 2005


Christian Hennig wrote:

>>run_rows<-seq(0,1,0.05)
>>run_cols<-seq(0.3,0.6,0.05)
>>
>>res<-matrix(NA,length(run_rows),length(run_cols))
>>
>>for(i in 1:length(run_rows))
>>{
>>    for(j in 1:length(run_cols))
>>    {
>>        res[i,j]=run_rows[i]+run_cols[j]
>>    }
>>}

Or the one-liner:

res = outer(run_rows,run_cols,"+")

but maybe its good to learn about array indexing first...

Barry




More information about the R-help mailing list