r - Apply - return results binded by rows not by columns -


i have function applied on vector of lenght 5 returns matrix 4 rows , 5 columns. want use apply() in order call function again on each row of results matrix , obtain matrix 16 (4*4) rows , 5 columns. unfortuneately apply() combines results 4x20 matrix. how possible change without using lists?

matrixfromvector = function(x){  return(rbind(x*rnorm(1,1,.01),x*rnorm(1,1,.01),x*rnorm(1,1,.1),x*rnorm(1,1,.01))) }  = matrixfromvector(1:5) t(a)          [,1]     [,2]     [,3]      [,4] [1,] 1.008391 1.005974 1.077223 0.9865611 [2,] 2.016782 2.011947 2.154445 1.9731222 [3,] 3.025173 3.017921 3.231668 2.9596833 [4,] 4.033565 4.023894 4.308890 3.9462444 [5,] 5.041956 5.029868 5.386113 4.9328055 

after applying function each row of have

            [1,]        [2,]        [3,]        [4,]        [5,] [1,]    1.0242459   2.0484917   3.0727376   4.0969835   5.1212293 [2,]    0.9999314   1.9998629   2.9997943   3.9997257   4.9996572 [3,]    1.0836573   2.1673146   3.2509719   4.3346292   5.4182865 [4,]    1.0005137   2.0010275   3.0015412   4.0020550   5.0025687 [5,]    1.0314108   2.0628216   3.0942323   4.1256431   5.1570539 [6,]    0.9995248   1.9990496   2.9985744   3.9980992   4.9976239 [7,]    1.0908017   2.1816034   3.2724051   4.3632069   5.4540086 [8,]    0.9801833   1.9603667   2.9405500   3.9207333   4.9009166 [9,]    0.9697334   1.9394669   2.9092003   3.8789338   4.8486672 [10,]   0.8484190   1.6968380   2.5452570   3.3936760   4.2420950 [11,]   0.9120351   1.8240703   2.7361054   3.6481405   4.5601756 [12,]   0.9596908   1.9193816   2.8790724   3.8387632   4.7984540 [13,]   1.0226757   2.0453515   3.0680272   4.0907030   5.1133787 [14,]   1.0069771   2.0139543   3.0209314   4.0279085   5.0348857 [15,]   1.0748773   2.1497545   3.2246318   4.2995090   5.3743863 [16,]   0.9841864   1.9683728   2.9525592   3.9367456   4.9209319 

instead got

apply(a,1,matrixfromvector)        [,1]      [,2]     [,3]      [,4]  [1,] 1.0262524 1.0237143 1.074673 0.9885002  [2,] 0.9990472 1.0189053 1.062644 0.9965570  [3,] 0.9464976 0.8973152 1.138847 0.8639614  [4,] 1.0063561 1.0080947 1.080825 1.0033793   [5,] 2.0525048 2.0474286 2.149346 1.9770004  [6,] 1.9980944 2.0378107 2.125288 1.9931140  [7,] 1.8929952 1.7946303 2.277693 1.7279229  [8,] 2.0127121 2.0161895 2.161650 2.0067587   [9,] 3.0787573 3.0711429 3.224019 2.9655005 [10,] 2.9971416 3.0567160 3.187933 2.9896710 [11,] 2.8394929 2.6919455 3.416540 2.5918843 [12,] 3.0190682 3.0242842 3.242475 3.0101380  [13,] 4.1050097 4.0948572 4.298693 3.9540007 [14,] 3.9961888 4.0756214 4.250577 3.9862280 [15,] 3.7859905 3.5892607 4.555386 3.4558457 [16,] 4.0254242 4.0323789 4.323300 4.0135174  [17,] 5.1312621 5.1185715 5.373366 4.9425009 [18,] 4.9952359 5.0945267 5.313221 4.9827850 [19,] 4.7324881 4.4865759 5.694233 4.3198072 [20,] 5.0317803 5.0404736 5.404125 5.0168967 

or

apply(a,1,function(x) t(matrixfromvector(x)))        [,1]      [,2]     [,3]      [,4]  [1,] 1.0242459 0.9999314 1.0836573 1.0005137  [2,] 2.0484917 1.9998629 2.1673146 2.0010275  [3,] 3.0727376 2.9997943 3.2509719 3.0015412  [4,] 4.0969835 3.9997257 4.3346292 4.0020550  [5,] 5.1212293 4.9996572 5.4182865 5.0025687   [6,] 1.0314108 0.9995248 1.0908017 0.9801833  [7,] 2.0628216 1.9990496 2.1816034 1.9603667  [8,] 3.0942323 2.9985744 3.2724051 2.9405500  [9,] 4.1256431 3.9980992 4.3632069 3.9207333 [10,] 5.1570539 4.9976239 5.4540086 4.9009166  [11,] 0.9697334 0.8484190 0.9120351 0.9596908 [12,] 1.9394669 1.6968380 1.8240703 1.9193816 [13,] 2.9092003 2.5452570 2.7361054 2.8790724 [14,] 3.8789338 3.3936760 3.6481405 3.8387632 [15,] 4.8486672 4.2420950 4.5601756 4.7984540  [16,] 1.0226757 1.0069771 1.0748773 0.9841864 [17,] 2.0453515 2.0139543 2.1497545 1.9683728 [18,] 3.0680272 3.0209314 3.2246318 2.9525592 [19,] 4.0907030 4.0279085 4.2995090 3.9367456 [20,] 5.1133787 5.0348857 5.3743863 4.9209319     

we can loop on rows using lapply , this

do.call(rbind, lapply(seq_len(nrow(a)), function(i)  matrixfromvector(a[i,]))) 

or place output in list using apply , rbind

do.call(rbind, do.call(c, apply(a, 1, function(x) list(matrixfromvector(x))))) 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -