how to make R language result display in one line [duplicate] - r

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Increase the width of matrix printout
I have a big matrix named S, so it displays results separated across multiple lines. This is not clear for me, so is there a way to display the whole matrix, and not let the columns print on separated lines like this:
> S
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 1.0000000 0.5393599 0.5276449 0.33449680 0.2925090 0.60927180 0.2925090
[2,] 0.5393599 1.0000000 0.7826238 0.43412157 0.6507914 0.51639778 0.5423261
[3,] 0.5276449 0.7826238 1.0000000 0.41602515 0.5457052 0.50518149 0.5457052
[4,] 0.3344968 0.4341216 0.4160251 1.00000000 0.2690691 0.08006408 0.3363364
[5,] 0.2925090 0.6507914 0.5457052 0.26906912 1.0000000 0.42008403 0.5294118
[6,] 0.6092718 0.5163978 0.5051815 0.08006408 0.4200840 1.00000000 0.4900980
[7,] 0.2925090 0.5423261 0.5457052 0.33633640 0.5294118 0.49009803 1.0000000
[8,] 0.4029115 0.5378529 0.6013378 0.44474959 0.6482037 0.46291005 0.5185630
[9,] 0.3636364 0.5393599 0.5276449 0.16724840 0.6581452 0.52223297 0.4387635
[10,] 0.2727273 0.4045199 0.4522670 0.25087260 0.5850179 0.43519414 0.6581452
[11,] 0.4351941 0.6454972 0.5773503 0.32025631 0.4900980 0.41666667 0.4200840
[12,] 0.3636364 0.6741999 0.5276449 0.33449680 0.5850179 0.34815531 0.4387635
[13,] 0.1906925 0.2828427 0.1581139 0.43852901 0.3834825 0.18257419 0.3834825
[,8] [,9] [,10] [,11] [,12] [,13]
[1,] 0.4029115 0.3636364 0.2727273 0.4351941 0.3636364 0.1906925
[2,] 0.5378529 0.5393599 0.4045199 0.6454972 0.6741999 0.2828427
[3,] 0.6013378 0.5276449 0.4522670 0.5773503 0.5276449 0.1581139
[4,] 0.4447496 0.1672484 0.2508726 0.3202563 0.3344968 0.4385290
[5,] 0.6482037 0.6581452 0.5850179 0.4900980 0.5850179 0.3834825
[6,] 0.4629100 0.5222330 0.4351941 0.4166667 0.3481553 0.1825742
[7,] 0.5185630 0.4387635 0.6581452 0.4200840 0.4387635 0.3834825
[8,] 1.0000000 0.6446584 0.5640761 0.4629100 0.5640761 0.4225771
[9,] 0.6446584 1.0000000 0.6363636 0.3481553 0.4545455 0.2860388
[10,] 0.5640761 0.6363636 1.0000000 0.2611165 0.3636364 0.4767313
[11,] 0.4629100 0.3481553 0.2611165 1.0000000 0.5222330 0.2738613
[12,] 0.5640761 0.4545455 0.3636364 0.5222330 1.0000000 0.1906925
[13,] 0.4225771 0.2860388 0.4767313 0.2738613 0.1906925 1.0000000

In addition to the print(... ,digits) method you can also change the width at which printing wraps:
options(width = 150)

Make your window wide...
And, depending on the number of digits that count try...
print(S, digits = 3)
but you really need to come up with better ways of examining correlation matrices that don't depend on such things.

Related

R - how to use outer

I have an easy problem but I can't figure it out. I want to create a multiplication table using outer, but I want result to be for example 5x7, instead of 35.
I tried:
a <- paste(1:10,collapse="x")
b <- 1:10
outer(a,b)
but it doesn't work, I guess because my "a" isn't a vector. How can I do it?
Try this:
a <- 1:10
b <- 1:10
outer(a,b,FUN=paste, sep="x" )
Output:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "1x1" "1x2" "1x3" "1x4" "1x5" "1x6" "1x7" "1x8" "1x9" "1x10"
[2,] "2x1" "2x2" "2x3" "2x4" "2x5" "2x6" "2x7" "2x8" "2x9" "2x10"
[3,] "3x1" "3x2" "3x3" "3x4" "3x5" "3x6" "3x7" "3x8" "3x9" "3x10"
[4,] "4x1" "4x2" "4x3" "4x4" "4x5" "4x6" "4x7" "4x8" "4x9" "4x10"
[5,] "5x1" "5x2" "5x3" "5x4" "5x5" "5x6" "5x7" "5x8" "5x9" "5x10"
[6,] "6x1" "6x2" "6x3" "6x4" "6x5" "6x6" "6x7" "6x8" "6x9" "6x10"
[7,] "7x1" "7x2" "7x3" "7x4" "7x5" "7x6" "7x7" "7x8" "7x9" "7x10"
[8,] "8x1" "8x2" "8x3" "8x4" "8x5" "8x6" "8x7" "8x8" "8x9" "8x10"
[9,] "9x1" "9x2" "9x3" "9x4" "9x5" "9x6" "9x7" "9x8" "9x9" "9x10"
[10,] "10x1" "10x2" "10x3" "10x4" "10x5" "10x6" "10x7" "10x8" "10x9" "10x10"
I guess what you want to do is like below
a <- sprintf("%sx", 1:10)
b <- 1:10
outer(a, b, paste0)
giving
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "1x1" "1x2" "1x3" "1x4" "1x5" "1x6" "1x7" "1x8" "1x9" "1x10"
[2,] "2x1" "2x2" "2x3" "2x4" "2x5" "2x6" "2x7" "2x8" "2x9" "2x10"
[3,] "3x1" "3x2" "3x3" "3x4" "3x5" "3x6" "3x7" "3x8" "3x9" "3x10"
[4,] "4x1" "4x2" "4x3" "4x4" "4x5" "4x6" "4x7" "4x8" "4x9" "4x10"
[5,] "5x1" "5x2" "5x3" "5x4" "5x5" "5x6" "5x7" "5x8" "5x9" "5x10"
[6,] "6x1" "6x2" "6x3" "6x4" "6x5" "6x6" "6x7" "6x8" "6x9" "6x10"
[7,] "7x1" "7x2" "7x3" "7x4" "7x5" "7x6" "7x7" "7x8" "7x9" "7x10"
[8,] "8x1" "8x2" "8x3" "8x4" "8x5" "8x6" "8x7" "8x8" "8x9" "8x10"
[9,] "9x1" "9x2" "9x3" "9x4" "9x5" "9x6" "9x7" "9x8" "9x9" "9x10"
[10,] "10x1" "10x2" "10x3" "10x4" "10x5" "10x6" "10x7" "10x8" "10x9" "10x10"

Solving a system equation with R

This is the Markowitz's efficient portfolio problem, but the "original". I need to maximize the return of a portfolio subject to a level of risk.
I have a vector mu of returns:
mu
[,1] [,2] [,3] [,4] [,5] [,6]
2020-11-11 0.0002720645 0.000436814 0.0001976725 7.367183e-05 0.0001061771 2.123921e-05
[,7] [,8] [,9] [,10] [,11] [,12]
2020-11-11 0.0002674939 -7.217231e-05 7.246612e-05 0.0003106428 0.0002488269 -9.916666e-05
[,13] [,14] [,15] [,16] [,17] [,18]
2020-11-11 0.0001324967 0.000121239 0.0001060435 0.0002293328 0.0001029351 8.083295e-05
A variance covariance matrix:
sigma
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 5.592601e-05 1.867019e-07 -1.928308e-06 6.144346e-07 -2.773157e-07 1.659026e-05
[2,] 1.867019e-07 2.484421e-06 3.387304e-07 -1.116725e-07 4.748165e-08 6.849967e-06
[3,] -1.928308e-06 3.387304e-07 1.203656e-06 -5.667758e-08 1.085685e-07 -3.056249e-07
[4,] 6.144346e-07 -1.116725e-07 -5.667758e-08 2.100015e-08 -6.407951e-09 -5.022572e-08
[5,] -2.773157e-07 4.748165e-08 1.085685e-07 -6.407951e-09 2.156967e-07 3.545964e-09
[6,] 1.659026e-05 6.849967e-06 -3.056249e-07 -5.022572e-08 3.545964e-09 5.226998e-05
[7,] 1.801405e-05 8.490776e-06 -5.241004e-07 1.107834e-07 6.741364e-09 3.427730e-05
[8,] 4.081106e-05 2.616975e-06 -2.783963e-06 6.162108e-07 -2.925430e-07 4.131267e-05
[9,] 5.406343e-08 -9.440572e-09 -5.508222e-09 1.364631e-09 -5.884534e-10 -3.247901e-09
[10,] 1.436211e-05 6.552363e-06 -1.738885e-07 -1.002123e-07 1.232430e-08 3.661951e-05
[11,] 9.153688e-06 4.140858e-07 -4.691999e-07 1.450149e-07 -5.655290e-08 6.904077e-06
[12,] 2.503943e-05 9.261196e-07 -6.080288e-07 3.882300e-07 -8.190216e-08 1.727989e-05
[13,] 2.346539e-06 -2.721675e-07 -1.601445e-08 5.728516e-08 -4.386966e-09 2.463735e-07
[14,] 1.231017e-06 -2.161433e-07 -1.686978e-08 3.623457e-08 3.462555e-08 -1.252470e-07
[15,] 2.803784e-06 -4.366683e-07 1.349062e-07 1.065253e-07 1.319820e-08 -2.084133e-07
[16,] 5.000938e-06 -8.268924e-07 6.099295e-07 1.324316e-07 5.926589e-08 -9.965225e-07
[17,] -1.062064e-07 2.265318e-08 2.875487e-08 -3.366920e-09 1.776274e-09 4.806526e-09
[18,] -4.585421e-07 6.782657e-08 3.215220e-07 -1.258659e-08 2.188159e-08 -1.362058e-07
[,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1.801405e-05 4.081106e-05 5.406343e-08 1.436211e-05 9.153688e-06 2.503943e-05
[2,] 8.490776e-06 2.616975e-06 -9.440572e-09 6.552363e-06 4.140858e-07 9.261196e-07
[3,] -5.241004e-07 -2.783963e-06 -5.508222e-09 -1.738885e-07 -4.691999e-07 -6.080288e-07
[4,] 1.107834e-07 6.162108e-07 1.364631e-09 -1.002123e-07 1.450149e-07 3.882300e-07
[5,] 6.741364e-09 -2.925430e-07 -5.884534e-10 1.232430e-08 -5.655290e-08 -8.190216e-08
[6,] 3.427730e-05 4.131267e-05 -3.247901e-09 3.661951e-05 6.904077e-06 1.727989e-05
[7,] 5.237200e-05 3.680683e-05 5.354780e-09 2.968337e-05 7.161037e-06 1.813164e-05
[8,] 3.680683e-05 1.662421e-04 5.065368e-08 7.083157e-05 1.028446e-05 2.650823e-05
[9,] 5.354780e-09 5.065368e-08 1.520614e-10 -7.552573e-09 1.266719e-08 3.327868e-08
[10,] 2.968337e-05 7.083157e-05 -7.552573e-09 4.900492e-05 4.501299e-06 1.198971e-05
[11,] 7.161037e-06 1.028446e-05 1.266719e-08 4.501299e-06 1.493544e-05 2.091619e-05
[12,] 1.813164e-05 2.650823e-05 3.327868e-08 1.198971e-05 2.091619e-05 6.634911e-05
[13,] 5.699686e-07 1.961050e-06 4.834209e-09 -6.040182e-08 2.463262e-06 4.880533e-06
[14,] 1.583679e-07 1.116495e-06 3.072008e-09 -2.197953e-07 2.903155e-07 8.507265e-07
[15,] 7.753749e-07 2.291707e-06 4.353285e-09 -5.523707e-07 6.641738e-07 2.131070e-06
[16,] 2.247954e-07 3.167986e-06 1.075832e-08 -1.326120e-06 9.988603e-07 3.492155e-06
[17,] -9.016614e-09 -1.176373e-07 -3.044293e-10 1.502323e-08 -2.420553e-08 -4.921572e-08
[18,] -1.588521e-07 -7.068861e-07 -1.222680e-09 -1.076004e-07 -9.341448e-08 -9.692232e-08
[,13] [,14] [,15] [,16] [,17] [,18]
[1,] 2.346539e-06 1.231017e-06 2.803784e-06 5.000938e-06 -1.062064e-07 -4.585421e-07
[2,] -2.721675e-07 -2.161433e-07 -4.366683e-07 -8.268924e-07 2.265318e-08 6.782657e-08
[3,] -1.601445e-08 -1.686978e-08 1.349062e-07 6.099295e-07 2.875487e-08 3.215220e-07
[4,] 5.728516e-08 3.623457e-08 1.065253e-07 1.324316e-07 -3.366920e-09 -1.258659e-08
[5,] -4.386966e-09 3.462555e-08 1.319820e-08 5.926589e-08 1.776274e-09 2.188159e-08
[6,] 2.463735e-07 -1.252470e-07 -2.084133e-07 -9.965225e-07 4.806526e-09 -1.362058e-07
[7,] 5.699686e-07 1.583679e-07 7.753749e-07 2.247954e-07 -9.016614e-09 -1.588521e-07
[8,] 1.961050e-06 1.116495e-06 2.291707e-06 3.167986e-06 -1.176373e-07 -7.068861e-07
[9,] 4.834209e-09 3.072008e-09 4.353285e-09 1.075832e-08 -3.044293e-10 -1.222680e-09
[10,] -6.040182e-08 -2.197953e-07 -5.523707e-07 -1.326120e-06 1.502323e-08 -1.076004e-07
[11,] 2.463262e-06 2.903155e-07 6.641738e-07 9.988603e-07 -2.420553e-08 -9.341448e-08
[12,] 4.880533e-06 8.507265e-07 2.131070e-06 3.492155e-06 -4.921572e-08 -9.692232e-08
[13,] 8.999334e-07 1.321428e-07 3.261184e-07 6.050874e-07 -6.496856e-09 9.309784e-09
[14,] 1.321428e-07 9.120545e-08 1.986952e-07 3.772898e-07 -4.786053e-09 7.777481e-09
[15,] 3.261184e-07 1.986952e-07 8.154712e-07 1.007473e-06 -5.816547e-09 6.094590e-08
[16,] 6.050874e-07 3.772898e-07 1.007473e-06 2.313540e-06 -2.842490e-09 2.360389e-07
[17,] -6.496856e-09 -4.786053e-09 -5.816547e-09 -2.842490e-09 1.682421e-09 8.910643e-09
[18,] 9.309784e-09 7.777481e-09 6.094590e-08 2.360389e-07 8.910643e-09 1.407667e-07
And I want to solve the following maximization problem:
Where mu_p is the portfolio return (weighted average), sigma_p**2 is the variance of the portfolio, SIGMA is the varcov matrix, 1 is a vector of 1 and x are the weights
Plus a constraint that make all values of x greater or equal than cero
I really don't know how to do that in R, I have review some packages like ROI and CVXR, but I don't understand how to solve this.

Apply - return results binded by rows not by columns

I have a function which applied on a vector of lenght 5 returns a matrix with 4 rows and 5 columns. Then I want to use apply() in order to call my function again on each row of the results matrix and obtain matrix with 16 (4*4) rows and 5 columns. Unfortuneately apply() combines the results into 4x20 matrix. How is it possible to change that 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))) }
a = 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 my function to each row of a I would like to 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 I 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 over the rows using lapply and then do this
do.call(rbind, lapply(seq_len(nrow(a)), function(i) matrixFromVector(a[i,])))
Or we place the output in a list using apply and then do the rbind
do.call(rbind, do.call(c, apply(a, 1, function(x) list(matrixFromVector(x)))))
why not
apply(t(a), 1, matrixFromVector)
or
apply(a, 2, matrixFromVector)

Extracting element from list of lists in R?

I have a list in the following format:
[[825]][[4]]
Each of the 4 inside list elements are different sized and dimensioned arrays:
[[1]]
[1] 0.02918644 0.03239657 0.03560670 0.03881683 0.04202696 0.04523709 0.04844722 0.05165735
[9] 0.05486748 0.05807761 0.06128774 0.06449787 0.06770800 0.07091813 0.07412827 0.07733840
[17] 0.08054853 0.08375866 0.08696879 0.09017892
[[2]]
[1] 0.7581078 0.7587820 0.7608009 0.7641538 0.7688234 0.7747857 0.7820113 0.7904655 0.8001093
[10] 0.8109003 0.8244816 0.8444896 0.8706241 0.9023530 0.9391094 0.9803280 1.0254709 1.0740433
[19] 1.1256013 1.1797536
[[3]]
[,1] [,2] [,3]
[1,] 0.4177711 0.34606863 2.361603e-01
[2,] 0.4345125 0.35491274 2.105747e-01
[3,] 0.4512540 0.36375685 1.849892e-01
[4,] 0.4679954 0.37260096 1.594036e-01
[5,] 0.4847369 0.38144507 1.338180e-01
[6,] 0.5014783 0.39028918 1.082325e-01
[7,] 0.5182198 0.39913329 8.264693e-02
[8,] 0.5349612 0.40797740 5.706137e-02
[9,] 0.5517027 0.41682150 3.147581e-02
[10,] 0.5684441 0.42566561 5.890257e-03
[11,] 0.6059978 0.39400216 0.000000e+00
[12,] 0.6497759 0.35022414 0.000000e+00
[13,] 0.6935539 0.30644612 0.000000e+00
[14,] 0.7373319 0.26266811 -2.408519e-18
[15,] 0.7811099 0.21889009 -6.394265e-19
[16,] 0.8248879 0.17511207 1.129666e-18
[17,] 0.8686659 0.13133405 2.898758e-18
[18,] 0.9124440 0.08755604 4.667850e-18
[19,] 0.9562220 0.04377802 6.436942e-18
[20,] 1.0000000 0.00000000 0.000000e+00
[[4]]
[,1]
[1,] 0.03849906
[2,] 0.04269549
[3,] 0.04680160
[4,] 0.05079714
[5,] 0.05466400
[6,] 0.05838658
[7,] 0.06195207
[8,] 0.06535055
[9,] 0.06857498
[10,] 0.07162115
[11,] 0.07433489
[12,] 0.07637498
[13,] 0.07776951
[14,] 0.07859245
[15,] 0.07893464
[16,] 0.07889032
[17,] 0.07854784
[18,] 0.07798443
[19,] 0.07726429
[20,] 0.07643877
I want to have 4 new lists, each with 825 elements:
[[4]][[825]]
For example, all the [[1]]'s, [[2]]'s etc. from the list of 825 should be combined.
What's the best way to do this? I've been trying to figure it out with some sort of apply..
First create an example list of lists:
big.lst <- lapply(1:825, function(x) rep(list(rnorm(10)), 4))
#check lengths
length(big.lst)
#[1] 825
unique(lengths(big.lst))
#[1] 4
Then lapply a subset over the big list. I chose 1:4 to create four new groups, but you can genralize with 1:length(big.lst[[1]]) as each sublist has the same length:
newlst <- lapply(1:4, function(x) lapply(big.lst, '[[', x))
#verify answer
length(newlst)
#[1] 4
unique(lengths(newlst))
#[1] 825

How to calculate random numbers in R

I have a matrix with 26 columns. The values in each row sum up to 1:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,] 0.02105263 0.01052632 0.01052632 0.04210526 0.01052632 0.06315789 0.03157895 0.1789474 0.07368421 0.07368421 0.02105263
[2,] 0.00000000 0.01176471 0.01176471 0.00000000 0.01176471 0.18823529 0.09411765 0.1764706 0.15294118 0.07058824 0.01176471
[3,] 0.00000000 0.00000000 0.02941176 0.01470588 0.04411765 0.11764706 0.05882353 0.2058824 0.07352941 0.08823529 0.00000000
[,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22]
[1,] 0.04210526 0.04210526 0.05263158 0 0.03157895 0.02105263 0.00000000 0.04210526 0.01052632 0.05263158 0.02105263
[2,] 0.00000000 0.01176471 0.00000000 0 0.03529412 0.01176471 0.04705882 0.04705882 0.02352941 0.01176471 0.00000000
[3,] 0.02941176 0.02941176 0.02941176 0 0.05882353 0.01470588 0.02941176 0.02941176 0.02941176 0.01470588 0.00000000
[,23] [,24] [,25] [,26]
[1,] 0.06315789 0.03157895 0.03157895 0.02105263
[2,] 0.05882353 0.02352941 0.00000000 0.00000000
[3,] 0.02941176 0.01470588 0.00000000 0.05882353
I would like to alternate the values to make up some new data. This would mean changing every value in a row randomly to a value in the range of +- 5%, while still adding up to 1 with the rowsum.
So in column2 the 6th value is currently 0.18 and in the new data it should be somewhere between 0.171 and 0.189 (and plus 5%).
Alternatively, the value in the column should just be drawn from a normal distribution, but should not differ too much from the original value. Maybe more for large values like 0.18 and also for values which are smaller.
If the value is 0, it would be good to randomly decide whether it should stay at 0 or increase by a range between 5% or 10% (taking as the initial value something like 0.0001).
Is there an easy way to do this?
Well, the first thing you want to do is be able to generate new numbers. It can be done with rnorm(). You can supply it with the mean and standard deviation. The mean should be zero and sd somewhere around 0.02 or so. It would result in vast majority of generated numbers to be within 0.05 of the original number.
After that you want to re-scale back to a rowsum of 1, which is easily achieved by dividing all values with the sum of the whole row.
> (a <- 1:10)
[1] 1 2 3 4 5 6 7 8 9 10
> (a <- a / sum(a))
[1] 0.01818182 0.03636364 0.05454545 0.07272727 0.09090909 0.10909091 0.12727273 0.14545455 0.16363636 0.18181818
> (a <- a + rnorm(10, 0, 0.02))
[1] 0.01293189 0.06799608 0.03552480 0.08015437 0.07834294 0.07845255 0.11692691 0.13262836 0.15728399 0.16228330
> sum(a)
[1] 0.9225252
> sum(a / sum(a))
[1] 1
> a <- a / sum(a)
I'll leave it to you to figure out how to eliminate negative numbers and the 5% or 10% increase. But those are the tools you need.
Let your dataset be a matrix called data, then data * matrix(runif(prod(dim(data)),.95,1.05),nrow=nrow(data)) will give you data that is all +/- 5%.
If you don't want negative values you can wrap it all in an abs() since if a value can shift by 5% and be negative, the absolute value will always be within 5% of the original value still.
If you want to start with no 0 values then step one is data = data[which(data<=0)] = 0.001

Resources