songfengli f<-fumction(x){<br /> y<-x^2<br /> }<br /> x<-seq(2,1000,2)<br /> a<-numeric(500)<br /> for (i in 1:500)<br /> a[i]<-f(x[i])<br /> a 这样也可以吧 ans=sapply(1:500,function(x)...) 举个例子好吗?我运行的怎么不行。谢谢!
rtist [quote]引用第4楼abel于2006-12-27 10:17发表的“”: rtist兄,这是替代循环的方式之一吧,您那边有没有关于显式循环替代方法的资料呢?发现某本书上面有一节将过这个,但是没有看到具体的内容,要不麻烦您整理一下吧,这个主题还是特别有用的吧。[/quote] 其实这东西根本没啥用,就是给我这种懒人预备的,运行速度上通常比不上显示循环。 我常用的也就是apply,sapply,lapply,和papply了;有人也用tapply,我几乎从来不用。
rtist [quote]引用第5楼songfengli于2006-12-28 14:31发表的“”: f<-fumction(x){ y<-x^2 } x<-seq(2,1000,2) a<-numeric(500) for (i in 1:500) a<-f(x) a [/quote] <br /> f=function(x)x^2<br /> a=numeric(500)<br /> for(i in 1:500)a[i]=f(i*2) 以上等价与<br /> a=sapply(1:500*2,function(x)x^2)<br />
drewlee [quote]引用第7楼rtist于2006-12-30 17:30发表的“”: 其实这东西根本没啥用,就是给我这种懒人预备的,运行速度上通常比不上显示循环。 我常用的也就是apply,sapply,lapply,和papply了;有人也用tapply,我几乎从来不用。[/quote] 真的么?我还以为要比循环快,在拼命的用啊。
neige in sas, you can use BY statement to produce the output, then use outest or ODS to take them out, no need to use append