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.
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"
Following code create a date sequence of 10 years with 16 Day interval.
library(chron)
seq.dates("01/01/2008","12/31/2017", 16)
Output
[1] 01/01/08 01/17/08 02/02/08 02/18/08 03/05/08 03/21/08 04/06/08 04/22/08 05/08/08
[10] 05/24/08 06/09/08 06/25/08 07/11/08 07/27/08 08/12/08 08/28/08 09/13/08 09/29/08
[19] 10/15/08 10/31/08 11/16/08 12/02/08 12/18/08 **01/03/09** 01/19/09 02/04/09 02/20/09
[28] 03/08/09 03/24/09 04/09/09 04/25/09 05/11/09 ..........
........................
...........................
[208] 01/25/17 02/10/17 02/26/17 03/14/17 03/30/17 04/15/17 05/01/17 05/17/17 06/02/17
[217] 06/18/17 07/04/17 07/20/17 08/05/17 08/21/17 09/06/17 09/22/17 10/08/17 10/24/17
[226] 11/09/17 11/25/17 12/11/17 12/27/17
I want first entry for every year to be 1st January not the day which comes after 16 days from the last entry of previous year (BOLD entry in the example sequence) and subsequent entries accordingly.
A long way to do this would be creating date sequence for individual years separately then merging them in a single vector. I'm curious that is there any way to do this in a single line code.
How's this work for you. Uses sapply to pass a vector of starting points and then makes seq.dates do more limited sequences. The sapply function will simplify to an array if possible.
dates(sapply( seq.dates("01/01/2008", "01/01/2017", by="years") ,
function(x) seq.dates(x, to=x+365, by=16, length=23)))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 01/01/08 01/01/09 01/01/10 01/01/11 01/01/12 01/01/13 01/01/14 01/01/15
[2,] 01/17/08 01/17/09 01/17/10 01/17/11 01/17/12 01/17/13 01/17/14 01/17/15
[3,] 02/02/08 02/02/09 02/02/10 02/02/11 02/02/12 02/02/13 02/02/14 02/02/15
[4,] 02/18/08 02/18/09 02/18/10 02/18/11 02/18/12 02/18/13 02/18/14 02/18/15
[5,] 03/05/08 03/06/09 03/06/10 03/06/11 03/05/12 03/06/13 03/06/14 03/06/15
[6,] 03/21/08 03/22/09 03/22/10 03/22/11 03/21/12 03/22/13 03/22/14 03/22/15
[7,] 04/06/08 04/07/09 04/07/10 04/07/11 04/06/12 04/07/13 04/07/14 04/07/15
[8,] 04/22/08 04/23/09 04/23/10 04/23/11 04/22/12 04/23/13 04/23/14 04/23/15
[9,] 05/08/08 05/09/09 05/09/10 05/09/11 05/08/12 05/09/13 05/09/14 05/09/15
[10,] 05/24/08 05/25/09 05/25/10 05/25/11 05/24/12 05/25/13 05/25/14 05/25/15
[11,] 06/09/08 06/10/09 06/10/10 06/10/11 06/09/12 06/10/13 06/10/14 06/10/15
[12,] 06/25/08 06/26/09 06/26/10 06/26/11 06/25/12 06/26/13 06/26/14 06/26/15
[13,] 07/11/08 07/12/09 07/12/10 07/12/11 07/11/12 07/12/13 07/12/14 07/12/15
[14,] 07/27/08 07/28/09 07/28/10 07/28/11 07/27/12 07/28/13 07/28/14 07/28/15
[15,] 08/12/08 08/13/09 08/13/10 08/13/11 08/12/12 08/13/13 08/13/14 08/13/15
[16,] 08/28/08 08/29/09 08/29/10 08/29/11 08/28/12 08/29/13 08/29/14 08/29/15
[17,] 09/13/08 09/14/09 09/14/10 09/14/11 09/13/12 09/14/13 09/14/14 09/14/15
[18,] 09/29/08 09/30/09 09/30/10 09/30/11 09/29/12 09/30/13 09/30/14 09/30/15
[19,] 10/15/08 10/16/09 10/16/10 10/16/11 10/15/12 10/16/13 10/16/14 10/16/15
[20,] 10/31/08 11/01/09 11/01/10 11/01/11 10/31/12 11/01/13 11/01/14 11/01/15
[21,] 11/16/08 11/17/09 11/17/10 11/17/11 11/16/12 11/17/13 11/17/14 11/17/15
[22,] 12/02/08 12/03/09 12/03/10 12/03/11 12/02/12 12/03/13 12/03/14 12/03/15
[23,] 12/18/08 12/19/09 12/19/10 12/19/11 12/18/12 12/19/13 12/19/14 12/19/15
[,9] [,10]
[1,] 01/01/16 01/01/17
[2,] 01/17/16 01/17/17
[3,] 02/02/16 02/02/17
[4,] 02/18/16 02/18/17
[5,] 03/05/16 03/06/17
[6,] 03/21/16 03/22/17
[7,] 04/06/16 04/07/17
[8,] 04/22/16 04/23/17
[9,] 05/08/16 05/09/17
[10,] 05/24/16 05/25/17
[11,] 06/09/16 06/10/17
[12,] 06/25/16 06/26/17
[13,] 07/11/16 07/12/17
[14,] 07/27/16 07/28/17
[15,] 08/12/16 08/13/17
[16,] 08/28/16 08/29/17
[17,] 09/13/16 09/14/17
[18,] 09/29/16 09/30/17
[19,] 10/15/16 10/16/17
[20,] 10/31/16 11/01/17
[21,] 11/16/16 11/17/17
[22,] 12/02/16 12/03/17
[23,] 12/18/16 12/19/17
I was a bit surprised at this result since I thought the value would be a character matrix, but str shows it's a matrix of chron date elements. Can remove the apparent "matrix" (actually "dates" with a dimension attribute) structure with a call to c:
str(c(dates(sapply( seq.dates("01/01/2008", "01/01/2017", by="years") , function(x) seq.dates(x, to=x+365, by=16, length=23))) ))
'dates' num [1:230] 01/01/08 01/17/08 02/02/08 02/18/08 03/05/08 ...
- attr(*, "format")= chr "m/d/y"
- attr(*, "origin")= num [1:3] 1 1 1970
I have folder with images
C:/Users/admin/Downloads/mypicture
Here several images, I need read it.
I use library("EBImage")
x <- readImage("C:/Users/admin/Downloads/mypicture")
then get the error
Error in readImage("C:/Users/admin/Downloads/mypicture") :
Unable to determine type of C:/Users/admin/Downloads/mypicture: Filename extension missing.
How to read all pictures at once,
and then extract pixel array for each picture
# width and height of the original image
dim(x)[1:2]
# scale to a specific width and height
y <- resize(x, w = 200, h = 100)
# scale by 50%; the height is determined automatically so that
# the aspect ratio is preserved
y <- resize(x, dim(x)[1]/2)
# show the scaled image
display(y)
# extract the pixel array
z <- imageData(y)
How to do it?
here two images from my folder
I need to create pixel array
z <- array(y)
like this,
where marked name of picture kBFrf.jpg
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 0.4676471 0.5000000 0.5382353 0.5803922 0.6107843 0.6441176 0.6715686 0.6843137 0.6901961 0.7117647 0.7303922 0.7362745
[2,] 0.4735294 0.5078431 0.5441176 0.5745098 0.6186275 0.6392157 0.6774510 0.6882353 0.7127451 0.7245098 0.7500000 0.7764706
[3,] 0.4735294 0.5039216 0.5470588 0.5892157 0.6186275 0.6539216 0.6764706 0.7049020 0.7225490 0.7343137 0.7637255 0.7862745
[,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,] 0.7686275 0.7843137 0.8000000 0.8156863 0.8450980 0.8460784 0.8519608 0.8539216 0.8529412 0.8607843 0.8647059 0.8568627
[2,] 0.8049020 0.8274510 0.8392157 0.8745098 0.8843137 0.8892157 0.8960784 0.8882353 0.8911765 0.9009804 0.8950980 0.8872549
[3,] 0.8058824 0.8303922 0.8558824 0.8735294 0.8784314 0.8901961 0.8911765 0.8882353 0.8794118 0.8823529 0.8803922 0.8852941
69onL.jpg
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 0.4676471 0.5000000 0.5382353 0.5803922 0.6107843 0.6441176 0.6715686 0.6843137 0.6901961 0.7117647 0.7303922 0.7362745
[2,] 0.4735294 0.5078431 0.5441176 0.5745098 0.6186275 0.6392157 0.6774510 0.6882353 0.7127451 0.7245098 0.7500000 0.7764706
[3,] 0.4735294 0.5039216 0.5470588 0.5892157 0.6186275 0.6539216 0.6764706 0.7049020 0.7225490 0.7343137 0.7637255 0.7862745
[,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,] 0.7686275 0.7843137 0.8000000 0.8156863 0.8450980 0.8460784 0.8519608 0.8539216 0.8529412 0.8607843 0.8647059 0.8568627
[2,] 0.8049020 0.8274510 0.8392157 0.8745098 0.8843137 0.8892157 0.8960784 0.8882353 0.8911765 0.9009804 0.8950980 0.8872549
[3,] 0.8058824 0.8303922 0.8558824 0.8735294 0.8784314 0.8901961 0.8911765 0.8882353 0.8794118 0.8823529 0.8803922 0.8852941
How to create such dataframe
result for picture with digits
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19]
[1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.0000000 1.0000000
[2,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.0000000 1.0000000
[,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29]
[1,] 1.00000000 1.00000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000
readImage function takes as input a vector of file names, so in order to read all images from a directory you need to list its contents
library(EBImage)
# create a vector of file names
fls <- dir("./img", full.names = TRUE)
# read images
imgs <- readImage(fls, 'png')
# display images
display(imgs, method = 'raster', all = TRUE)
Image data is stored in an Image class which internally holds an array
# internal representation of images data
y <- class(imageData(imgs))
# dimensions of image stack
dim(imgs)
## [1] 825 825 4 10
The first two dimensions correspond to the XY dimensions, the third one holds color channels (RGBA in this case), and the last one are the individual images. In order to extract a single image, for example the second one, use
img <- getFrame(imgs, 2, "render")
If you would like to refer to images by their original names you could use an approach similar to the following.
# mapping of file names to frame indices
idx <- setNames(1:length(fls), basename(fls))
getFrame(imgs, idx['sample.png'], "render")
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)
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.