[R] apply/looping query

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Wed Jun 16 17:46:07 CEST 2004


Laura Quinn wrote:

> x=1:2881
> my.new.matrix<-matrix(nrow=2881,ncol=20)
> for(i in 1:20){
> my.new.matrix[[i]]<-approx(x,my.matrix[,i],n=2881)
> }
> 
> the error message says:
> 
> Error: more elements supplied than there are to replace
> 
> where am I going wrong??

approx() returns a list with $x and $y components

I'd use [,x] instead of [[i]] to replace columns.

Hence:

  for(i in 1:20){
   my.new.matrix[,i]<-approx(x,my.matrix[,i],n=2881)$y
  }

Baz




More information about the R-help mailing list