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"
Related
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.
Suppose I have a matrix M of 1000x20. I would like to form a new matrix M' using some particular row vectors of M. Suppose v is one of the row vectors where:
(sum(v[c(1,3,5)])<=m1) && (m2<= sum(v)) && (sum(v)<= m3)
m1, m2 and m3 are fixed real values.
You could use the builtin rowSums for this
M[rowSums(M[,c(1,3,5)]) <= m1 & m2 <= rowSums(M) & rowSums(M) <= m3,]
If I understand well, all the rows in M' should fulfill the three conditions.
Then
M2 <- M[apply(M, 1, function(v) all(sum(v[c(1,3,5)]) <= m1, sum(v) >= m2, sum(v) <= m3)),]
Example:
set.seed(100)
m1 <- 1.5; m2 <- .1; m3 <- 7
M <- matrix(runif(1000 * 20), ncol = 20)
M2 <- M[apply(M, 1, function(v) all(sum(v[c(1,3,5)]) <= m1, sum(v) >= m2, sum(v) <= m3)),]
M2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 0.25767250 0.11186636 0.767613322 0.1170775 0.351589511 0.661712083 0.03935602 0.46841819 0.4721064346 0.172146627 0.01752519 0.08527665
[2,] 0.77030161 0.27888573 0.467701486 0.5135113 0.204069268 0.125491520 0.06858947 0.12662916 0.7292570788 0.285208830 0.05650994 0.14870733
[3,] 0.07242114 0.78926973 0.243863937 0.2129602 0.123911744 0.319162785 0.31567318 0.41284578 0.1001742624 0.667738556 0.27429634 0.22873886
[4,] 0.15563612 0.73555998 0.566519791 0.2586319 0.003557601 0.004678758 0.35639824 0.22556914 0.8001102153 0.191661312 0.39477327 0.28339573
[5,] 0.11080007 0.60440136 0.214702301 0.9420645 0.101179283 0.108814211 0.07295065 0.02647395 0.1982432718 0.280463005 0.29672992 0.03595338
[6,] 0.03995796 0.01779683 0.034120481 0.4319075 0.173509366 0.237607285 0.23590851 0.63668298 0.2373327424 0.306213750 0.47806018 0.10314514
[7,] 0.33464360 0.15353447 0.160568010 0.1304614 0.624373973 0.296702323 0.30006204 0.44803001 0.0004048729 0.279024218 0.58307526 0.25563003
[8,] 0.28269347 0.79171254 0.006763404 0.8829949 0.139567362 0.554091424 0.07773655 0.36361459 0.0417987099 0.003237072 0.29995648 0.47039652
[9,] 0.41103685 0.04635594 0.076752019 0.1238780 0.105824207 0.225368397 0.85102284 0.87449559 0.2695279971 0.213470059 0.18801495 0.49205221
[,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
[1,] 0.003809129 0.170773590 0.4111454 0.53899309 0.001633953 0.16027479 0.69589323 0.36884988
[2,] 0.398553511 0.796394822 0.1390867 0.39390073 0.124383915 0.07680161 0.40494816 0.21593826
[3,] 0.168761350 0.007325687 0.6380575 0.46103552 0.076197875 0.04746372 0.01994033 0.71738844
[4,] 0.888398457 0.969297715 0.3354494 0.03448252 0.227974761 0.18020713 0.12183392 0.04599925
[5,] 0.349111641 0.794708739 0.6964420 0.04796040 0.147109043 0.30419784 0.13573959 0.32682619
[6,] 0.227472877 0.404970699 0.6489886 0.30762523 0.945180172 0.45424866 0.29609562 0.45801963
[7,] 0.983473312 0.650270978 0.4376916 0.11810879 0.190460034 0.06142249 0.57051288 0.36678108
[8,] 0.114269206 0.163538058 0.1640480 0.39256770 0.607522648 0.09782059 0.17926967 0.91380213
[9,] 0.510501551 0.532380254 0.3818165 0.23163991 0.631005005 0.16757878 0.08121266 0.44285992
Consider a vector of numbers , a <- c(75,26,65,27,97,72)
And a matrix 10x6 matrix b
1.4168709 0.6253624 2.08645202 2.9475645 1.29317931 0.80175442
0.3669328 0.851852 0.57428245 2.8542504 1.40075478 0.01745655
6.1173956 1.6848444 1.05468424 0.3382552 1.1428774 0.41141215
2.8203602 0.9573334 0.22131122 0.4406137 0.07209113 0.17910147
0.102152 0.1779387 0.94915127 0.3516491 1.48272109 0.06037996
0.3124434 0.4892484 2.04443039 0.1251463 2.41507973 1.25367433
0.2154152 0.3951161 0.60410084 0.7551265 0.55764737 1.17793564
1.5451135 0.7764766 3.11515773 1.3519765 0.08916275 1.39969422
0.4018092 0.2432501 0.06470464 2.6173665 0.24696145 5.27272096
1.1683212 0.1258633 0.19431636 0.4160356 1.61775945 0.78849181
dput
b <- structure(c(1.41687091749774, 0.366932780481875, 6.11739562418232,
2.8203601760972, 0.102152034174651, 0.312443420290947, 0.215415194164962,
1.54511345728281, 0.401809234172106, 1.16832122397808, 0.625362366437912,
0.851851973640633, 1.68484436153414, 0.957333435262454, 0.177938693314666,
0.489248352590948, 0.395116138737649, 0.776476616387118, 0.243250062223524,
0.125863284132781, 2.08645202020619, 0.57428245106712, 1.05468423915856,
0.221311220899224, 0.949151266561806, 2.04443038991633, 0.604100843891501,
3.11515773070936, 0.0647046443940286, 0.194316359037562, 2.94756450172152,
2.85425036383753, 0.338255227074493, 0.440613748457464, 0.351649099495262,
0.125146273523569, 0.755126529331219, 1.35197646259786, 2.61736654663894,
0.416035552509129, 1.29317931454153, 1.40075477585735, 1.14287740174205,
0.072091125883162, 1.48272109049815, 2.41507973323081, 0.557647368015562,
0.0891627511009574, 0.246961451135576, 1.61775945491138, 0.80175441955164,
0.0174565480835137, 0.411412146408111, 0.179101474117488, 0.0603799588836676,
1.25367433010839, 1.17793564121695, 1.39969422101023, 5.27272095591089,
0.788491813423944), .Dim = c(10L, 6L))
My question is how do I multiply the vector a with matrix b, row wise. I know what b%*%a will do.
I am trying to do something like this
75*1.4168709 + 26*0.6253624 + 65*2.08645202 + 27*2.9475645 + 97*1.29317931 + 72*0.80175442
75*0.3669328 + 26*0.851852 + 65*0.57428245 + 27*2.8542504 + 97*1.40075478 + 72*0.01745655
so on
Any suggestions are much appreciated.
Looks like a sweep-operation. In R for functions applied to a marging, "2" generally indicates a column operation, which from your argument and structures is how I would describe your expected result. n(I can see how you would call this "row-wise" but most R users would think of this as being applied "column-wise:.
> sweep(b,2,a,"*")
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 106.265319 16.259422 135.619381 79.584242 125.438394 57.726318
[2,] 27.519959 22.148151 37.328359 77.064760 135.873213 1.256871
[3,] 458.804672 43.805953 68.554476 9.132891 110.859108 29.621675
[4,] 211.527013 24.890669 14.385229 11.896571 6.992839 12.895306
[5,] 7.661403 4.626406 61.694832 9.494526 143.823946 4.347357
[6,] 23.433257 12.720457 132.887975 3.378949 234.262734 90.264552
[7,] 16.156140 10.273020 39.266555 20.388416 54.091795 84.811366
[8,] 115.883509 20.188392 202.485252 36.503364 8.648787 100.777984
[9,] 30.135693 6.324502 4.205802 70.668897 23.955261 379.635909
[10,] 87.624092 3.272445 12.630563 11.232960 156.922667 56.771411
Then just rowSums:
> rowSums( sweep(b,2,a,"*") )
[1] 520.8931 301.1913 720.7788 282.5876 231.6485 496.9479 224.9873 484.4873 514.9261 328.4541
Alternatively, the matrix operation:
a %*% t(b)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 520.8931 301.1913 720.7788 282.5876 231.6485 496.9479 224.9873 484.4873 514.9261 328.4541
And the slightly faster single function version:
tcrossprod(a,b)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 520.8931 301.1913 720.7788 282.5876 231.6485 496.9479 224.9873 484.4873 514.9261 328.4541
We can get the lengths same before doing the multiplication i.e. by replicating the 'a' elements
a[col(b)] * b
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 106.265319 16.259422 135.619381 79.584242 125.438394 57.726318
# [2,] 27.519959 22.148151 37.328359 77.064760 135.873213 1.256871
# [3,] 458.804672 43.805953 68.554476 9.132891 110.859108 29.621675
# [4,] 211.527013 24.890669 14.385229 11.896571 6.992839 12.895306
# [5,] 7.661403 4.626406 61.694832 9.494526 143.823946 4.347357
# [6,] 23.433257 12.720457 132.887975 3.378949 234.262734 90.264552
# [7,] 16.156140 10.273020 39.266555 20.388416 54.091795 84.811366
# [8,] 115.883509 20.188392 202.485252 36.503364 8.648787 100.777984
# [9,] 30.135693 6.324502 4.205802 70.668897 23.955261 379.635909
#[10,] 87.624092 3.272445 12.630563 11.232960 156.922667 56.771411
or transpose the 'b', then multiply with 'a' and transpose the output
t(t(b) * a)
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 106.265319 16.259422 135.619381 79.584242 125.438394 57.726318
# [2,] 27.519959 22.148151 37.328359 77.064760 135.873213 1.256871
# [3,] 458.804672 43.805953 68.554476 9.132891 110.859108 29.621675
# [4,] 211.527013 24.890669 14.385229 11.896571 6.992839 12.895306
# [5,] 7.661403 4.626406 61.694832 9.494526 143.823946 4.347357
# [6,] 23.433257 12.720457 132.887975 3.378949 234.262734 90.264552
# [7,] 16.156140 10.273020 39.266555 20.388416 54.091795 84.811366
# [8,] 115.883509 20.188392 202.485252 36.503364 8.648787 100.777984
# [9,] 30.135693 6.324502 4.205802 70.668897 23.955261 379.635909
#[10,] 87.624092 3.272445 12.630563 11.232960 156.922667 56.771411
Or replicate more explicitly with rep
rep(a, each = nrow(b)) * b
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 106.265319 16.259422 135.619381 79.584242 125.438394 57.726318
# [2,] 27.519959 22.148151 37.328359 77.064760 135.873213 1.256871
# [3,] 458.804672 43.805953 68.554476 9.132891 110.859108 29.621675
# [4,] 211.527013 24.890669 14.385229 11.896571 6.992839 12.895306
# [5,] 7.661403 4.626406 61.694832 9.494526 143.823946 4.347357
# [6,] 23.433257 12.720457 132.887975 3.378949 234.262734 90.264552
# [7,] 16.156140 10.273020 39.266555 20.388416 54.091795 84.811366
# [8,] 115.883509 20.188392 202.485252 36.503364 8.648787 100.777984
# [9,] 30.135693 6.324502 4.205802 70.668897 23.955261 379.635909
#[10,] 87.624092 3.272445 12.630563 11.232960 156.922667 56.771411
Or we could split the matrix 'b' by column into a list, and use that with mapply. Now, the corresponding individual units are multiplied
mapply(`*`, split(b, col(b)), a)
Once, we have completed the above step, just do rowSums
out2 <- rowSums(a[col(b)] * b)
out2
#[1] 520.8931 301.1913 720.7788 282.5876 231.6485 496.9479 224.9873 484.4873 514.9261 328.4541
-check the output with the OP's method
out1 <- (b%*%a)[,1]
out1
#[1] 520.8931 301.1913 720.7788 282.5876 231.6485 496.9479 224.9873 484.4873 514.9261 328.4541
all.equal(out1, out2)
#[1] TRUE
I am having a problem with text wrapping in code output chunks in knitr when knitting to HTML.
For example, if I run the following:
matrix(rnorm(60, 5, 2), ncol = 12)
The output in HTML will wrap the table, giving an output like this, where the 12th column is moved underneath the rest:
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## [1,] 3.407 0.8035 2.981 5.269 6.989 5.107 7.143 3.127 3.624 7.220 4.805
## [2,] 3.907 5.5971 5.488 4.995 6.496 5.980 1.576 3.009 6.605 3.440 2.754
## [3,] 1.945 3.7668 4.860 2.945 3.663 5.945 7.168 2.012 5.873 8.190 7.441
## [4,] 4.893 6.2054 4.403 3.967 2.880 7.196 1.813 3.283 5.216 5.699 2.829
## [5,] 5.706 0.9084 5.802 1.404 3.122 1.866 6.613 3.299 4.990 3.645 3.766
## [,12]
## [1,] 0.3951
## [2,] 4.0866
## [3,] 5.9293
## [4,] 6.4729
## [5,] 2.7172
Is there a method to adjust the width of the output chunk, so that I can have a table where the rows appear all on one line, like so?
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
## [1,] 3.407 0.8035 2.981 5.269 6.989 5.107 7.143 3.127 3.624 7.220 4.805 0.3951
## [2,] 3.907 5.5971 5.488 4.995 6.496 5.980 1.576 3.009 6.605 3.440 2.754 4.0866
## [3,] 1.945 3.7668 4.860 2.945 3.663 5.945 7.168 2.012 5.873 8.190 7.441 5.9293
## [4,] 4.893 6.2054 4.403 3.967 2.880 7.196 1.813 3.283 5.216 5.699 2.829 6.4729
## [5,] 5.706 0.9084 5.802 1.404 3.122 1.866 6.613 3.299 4.990 3.645 3.766 2.7172
Thanks!
Adding something like options(width=120) to your document would allow you to override the default wrapping width.
Be careful about going too wide though; when converting to PDF or other formats, the default is pretty much just right!
As an example, I use Knitr from RStudio, and type my document as a R markdown document. My document "options" at the start might be something like this:
```{r set-options, echo=FALSE, cache=FALSE}
options(width=80)
opts_chunk$set(comment = "", warning = FALSE, message = FALSE, echo = TRUE, tidy = TRUE, size="small")
read_chunk("some/script/I/want/to/load.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.