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
Related
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"
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
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)
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")
```