0
votes

I am trying to sample my matrix by columns and rows in R. I am only able to sample the matrix by columns:

mat_sampled <- mat[, sample(ncol(mat), size = 2), drop = FALSE]
mat_sampled

or by rows using:

mat_sampled <- mat[, sample(nrow(mat), size = 2), drop = FALSE]
mat_sampled

Anyway I can sample the matrix by the columns and rows such that I have a size = 2 and a 2x2 matrix?

Data:

mat <- structure(c(0.00000936737064850479, -0.00000344035877221533, 
-0.00000588429413064886, -0.00000664093882822385, 0.000000163142912622386, 
-0.00000830665821831891, 0.00000288029434887303, -0.00000738776901251938, 
-0.00000204722452402808, 0.00000316098482315733, -0.00000851960904025358, 
-0.00000344035877221533, 0.0000162661560266947, 0.000015667146245672, 
0.0000176817401578245, -0.000000434373913718343, 0.0000221167783645216, 
-0.00000766888802504515, 0.0000196702025728993, 0.0000054508094435003, 
-0.00000841623657913487, 0.0000226837676407729, -0.00000588429413064886, 
0.000015667146245672, 0.0000378271485255422, 0.0000302423574746387, 
-0.000000742941082669109, 0.0000378279236951252, -0.0000131166531696689, 
0.0000336433683844706, 0.00000932291314345129, -0.0000143948973882355, 
0.0000387976864121218, -0.00000664093882822385, 0.0000176817401578245, 
0.0000302423574746387, 0.0000609597329197104, -0.000000838473769909237, 
0.0000426921091434878, -0.0000148032864090012, 0.0000379694737305786, 
0.0000105217201098132, -0.0000162458964272219, 0.0000437865708985388, 
0.000000163142912622386, -0.000000434373913718343, -0.000000742941082669109, 
-0.000000838473769909237, 0.000000326052715384182, -0.00000104878469924473, 
0.000000363661121358915, -0.000000932767293205516, -0.000000258479126056273, 
0.000000399100628669316, -0.00000107567151195169, -0.00000830665821831891, 
0.0000221167783645216, 0.0000378279236951252, 0.0000426921091434878, 
-0.00000104878469924473, 0.0000764928881113762, -0.0000185163338931623, 
0.0000474932007608493, 0.0000131608399176306, -0.0000203207878527069, 
0.0000547693765014893, 0.00000288029434887303, -0.00000766888802504515, 
-0.0000131166531696689, -0.0000148032864090012, 0.000000363661121358915, 
-0.0000185163338931623, 0.0000520534673039822, -0.0000164680421616108, 
-0.00000456345883565746, 0.00000704613683126169, -0.0000189910215976678, 
-0.00000738776901251938, 0.0000196702025728993, 0.0000336433683844706, 
0.0000379694737305786, -0.000000932767293205516, 0.0000474932007608493, 
-0.0000164680421616108, 0.0000466935923927192, 0.0000117049772323336, 
-0.0000180728859744263, 0.0000487107440703871, -0.00000204722452402808, 
0.0000054508094435003, 0.00000932291314345129, 0.0000105217201098132, 
-0.000000258479126056273, 0.0000131608399176306, -0.00000456345883565746, 
0.0000117049772323336, 0.000012972556693963, -0.0000050081770726872, 
0.0000134982333199051, 0.00000316098482315733, -0.00000841623657913487, 
-0.0000143948973882355, -0.0000162458964272219, 0.000000399100628669316, 
-0.0000203207878527069, 0.00000704613683126169, -0.0000180728859744263, 
-0.0000050081770726872, 0.0000724199402378186, -0.0000208417348282368, 
-0.00000851960904025358, 0.0000226837676407729, 0.0000387976864121218, 
0.0000437865708985388, -0.00000107567151195169, 0.0000547693765014893, 
-0.0000189910215976678, 0.0000487107440703871, 0.0000134982333199051, 
-0.0000208417348282368, 0.0000808412506325906), .Dim = c(11L, 
11L), .Dimnames = list(c("IEF", "HYG", "DVY", "XLV", "SHY", "IXUS", 
"TLT", "IVE", "PFF", "IAU", "VXUS"), c("IEF", "HYG", "DVY", "XLV", 
"SHY", "IXUS", "TLT", "IVE", "PFF", "IAU", "VXUS")))

EDIT: I am trying to sample the following matrix:

Sample from mat

                  IEF              HYG              DVY              XLV              SHY
IEF   0.0000093673706 -0.0000034403588 -0.0000058842941 -0.0000066409388  0.0000001631429
HYG  -0.0000034403588  0.0000162661560  0.0000156671462  0.0000176817402 -0.0000004343739
DVY  -0.0000058842941  0.0000156671462  0.0000378271485  0.0000302423575 -0.0000007429411
XLV  -0.0000066409388  0.0000176817402  0.0000302423575  0.0000609597329 -0.0000008384738
SHY   0.0000001631429 -0.0000004343739 -0.0000007429411 -0.0000008384738  0.0000003260527
IXUS -0.0000083066582  0.0000221167784  0.0000378279237  0.0000426921091 -0.0000010487847
TLT   0.0000028802943 -0.0000076688880 -0.0000131166532 -0.0000148032864  0.0000003636611
IVE  -0.0000073877690  0.0000196702026  0.0000336433684  0.0000379694737 -0.0000009327673
PFF  -0.0000020472245  0.0000054508094  0.0000093229131  0.0000105217201 -0.0000002584791
IAU   0.0000031609848 -0.0000084162366 -0.0000143948974 -0.0000162458964  0.0000003991006
VXUS -0.0000085196090  0.0000226837676  0.0000387976864  0.0000437865709 -0.0000010756715
                IXUS              TLT              IVE              PFF              IAU
IEF  -0.000008306658  0.0000028802943 -0.0000073877690 -0.0000020472245  0.0000031609848
HYG   0.000022116778 -0.0000076688880  0.0000196702026  0.0000054508094 -0.0000084162366
DVY   0.000037827924 -0.0000131166532  0.0000336433684  0.0000093229131 -0.0000143948974
XLV   0.000042692109 -0.0000148032864  0.0000379694737  0.0000105217201 -0.0000162458964
SHY  -0.000001048785  0.0000003636611 -0.0000009327673 -0.0000002584791  0.0000003991006
IXUS  0.000076492888 -0.0000185163339  0.0000474932008  0.0000131608399 -0.0000203207879
TLT  -0.000018516334  0.0000520534673 -0.0000164680422 -0.0000045634588  0.0000070461368
IVE   0.000047493201 -0.0000164680422  0.0000466935924  0.0000117049772 -0.0000180728860
PFF   0.000013160840 -0.0000045634588  0.0000117049772  0.0000129725567 -0.0000050081771
IAU  -0.000020320788  0.0000070461368 -0.0000180728860 -0.0000050081771  0.0000724199402
VXUS  0.000054769377 -0.0000189910216  0.0000487107441  0.0000134982333 -0.0000208417348
                VXUS
IEF  -0.000008519609
HYG   0.000022683768
DVY   0.000038797686
XLV   0.000043786571
SHY  -0.000001075672
IXUS  0.000054769377
TLT  -0.000018991022
IVE   0.000048710744
PFF   0.000013498233
IAU  -0.000020841735
VXUS  0.000080841251

Where the expected output might look like:

            IVE           SHY
IVE    0.0000466935924  -0.0000009327673
SHY    -0.0000009327673  0.0000003260527

I am looking to take the random sample which would give me the same rows and columns, keeping the colnames and rownames.

1
I might be misunderstanding your question, but you could combine your two lines into one to sample both columns and rows at the same time: mat[sample(nrow(mat), size = 2), sample(ncol(mat), size = 2)]Callin
Your sampling by-row is incorrect: mat[, sample(nrow(mat), size = 2), drop = FALSE] provides random column indices based on the number of rows. If you want to sample rows, the sample call need to be before the first comma, not after.r2evans

1 Answers

4
votes

Adjusted to the edit.

If your rownames and colnames are the same, you can do this:

set.seed(0)
sub <- sample(colnames(mat), size = 2)
mat_sampled <- mat[sub,
                   sub, drop = FALSE]