R, Select a Subset of Matrix - r

Suppose I have 2 matrices m and m.ind. The first one contains actual data while the smaller one contains row indices.
m
[,1] [,2] [,3] [,4]
[1,] 0.23094453 -0.1379296 0.4173233 -1.7767756
[2,] -1.15772946 -0.1111935 1.0654023 0.6228674
[3,] 0.24707599 -0.6900143 0.9702020 -0.5222834
[4,] -0.09111356 -0.2217942 -0.1016292 1.3222310
[5,] 1.75737562 0.1829077 1.4032035 -0.3634403
m.ind
[,1] [,2] [,3] [,4]
[1,] 2 1 3 5
[2,] 2 2 4 4
I would like to create a subset of the matrix m whose row ids are the elements of the matrix m.ind.
m.sub
[,1] [,2] [,3] [,4]
[1,] -1.157729 -0.1379296 0.9702020 -0.3634403
[2,] -1.157729 -0.1111935 -0.1016292 1.3222310
I tried the function below:
test=apply(m, 2, function(x) x[m.ind])
Your assistance is appreciated.

You could produce the indices for subsetting m by either
m.ind2 <- m.ind + nrow(m)*(col(m.ind)-1)
or
m.ind2 <- cbind(as.vector(m.ind),as.vector(col(m.ind)))
And then do:
matrix(m[m.ind2],nrow=nrow(m.ind))
This solution employs the fact that matrix in R is essentially a vector with dim attributes.
Here is a sample data set:
set.seed(1)
m <- matrix(runif(20),ncol=4)
m.ind <- matrix(c(2,2,1,2,3,4,5,4),nrow=2)
and output:
[,1] [,2] [,3] [,4]
[1,] 0.3721239 0.8983897 0.6870228 0.7774452
[2,] 0.3721239 0.9446753 0.3841037 0.3800352

Related

Matrix calculation between list objects in R

I have created list of objects in R as follows:
set.seed(1234)
data <- matrix(rnorm(3*4,mean=0,sd=1), 3, 4)
results <- lapply(1:ncol(data), function(i) outer(data[, i], data[, i]))
all 4 list objects have dim=3x3. I also have the following matrix matr <- matrix(c(2,4,6,8),ncol=4), where each value corresponds to the above list objects.
Then, I use this equation matr[,1]*matr[,2]*results[[1]]*results[[2]] between the first two objects in order to create the below matrix
[,1] [,2] [,3]
[1,] 64.135122 2.6966755 12.4307531
[2,] 2.696676 0.1133865 0.5226732
[3,] 12.430753 0.5226732 2.4093448
How can I calculate the above equation for all all possible object combinations and save them to a new list?
We can use combn to create pairwise combination on the sequence of the list, extract the elements and do the multiplication
new_lst <- combn(seq_along(results), 2, \(i) matr[,i[1]] * matr[,i[2]] *
results[[i[1]]] * results[[i[2]]], simplify = FALSE)
names(new_lst) <- combn(seq_along(results), 2, paste, collapse="_")
-output
> new_lst
$`1_2`
[,1] [,2] [,3]
[1,] 64.135122 2.6966755 12.4307531
[2,] 2.696676 0.1133865 0.5226732
[3,] 12.430753 0.5226732 2.4093448
$`1_3`
[,1] [,2] [,3]
[1,] 5.775451 -1.2624981 -5.095849
[2,] -1.262498 0.2759787 1.113939
[3,] -5.095849 1.1139391 4.496217
$`1_4`
[,1] [,2] [,3]
[1,] 18.46710 -2.275650 -18.610758
[2,] -2.27565 0.280422 2.293352
[3,] -18.61076 2.293352 18.755530
$`2_3`
[,1] [,2] [,3]
[1,] 43.621251 -7.589849 -9.242303
[2,] -7.589849 1.320590 1.608108
[3,] -9.242303 1.608108 1.958223
$`2_4`
[,1] [,2] [,3]
[1,] 139.47970 -13.680683 -33.754187
[2,] -13.68068 1.341852 3.310735
[3,] -33.75419 3.310735 8.168537
$`3_4`
[,1] [,2] [,3]
[1,] 12.560327 6.404863 13.837154
[2,] 6.404863 3.266019 7.055953
[3,] 13.837154 7.055953 15.243778

multiple matrix generation based on vectors in R

I have an (5x4) matrix in R, namely data defined as follows:
data <- matrix(rnorm(5*4,mean=0,sd=1), 5, 4)
and I want to create 4 different matrices that follows this formula: Assume that data[,1] = [A1,A2,A3,A4,A5]. I want to create the following matrix:
A1*A1 A1*A2 A1*A3 A1*A4 A1*A5
A2*A1 A2*A2 A2*A3 A2*A4 A2*A5
G1 = A3*A1 A3*A2 A3*A3 A3*A4 A3*A5
A4*A1 A4*A2 A4*A3 A4*A4 A4*A5
A5*A1 A5*A2 A5*A3 A5*A4 A5*A5
Similarly for the other columns i want to calculate at once all the G matrices (G1,G2,G3,G4). How can i achieve that?
results <- lapply(1:ncol(data), function(i) outer(data[, i], data[, i]))
results
[[1]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0.37164 0.37582 0.33424 -0.105387 0.120936
[2,] 0.37582 0.38006 0.33800 -0.106574 0.122298
[3,] 0.33424 0.33800 0.30060 -0.094780 0.108765
[4,] -0.10539 -0.10657 -0.09478 0.029885 -0.034294
[5,] 0.12094 0.12230 0.10876 -0.034294 0.039354
[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0.94684 0.117862 -1.01368 2.01456 0.719629
[2,] 0.11786 0.014671 -0.12618 0.25077 0.089579
[3,] -1.01368 -0.126183 1.08525 -2.15679 -0.770432
[4,] 2.01456 0.250772 -2.15679 4.28633 1.531132
[5,] 0.71963 0.089579 -0.77043 1.53113 0.546941
[[3]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1.61048 0.344159 -0.453466 2.68019 -0.57121
[2,] 0.34416 0.073547 -0.096906 0.57276 -0.12207
[3,] -0.45347 -0.096906 0.127684 -0.75467 0.16084
[4,] 2.68019 0.572758 -0.754669 4.46044 -0.95062
[5,] -0.57121 -0.122068 0.160837 -0.95062 0.20260
[[4]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0.559341 0.859297 0.451096 -0.0522063 -1.027929
[2,] 0.859297 1.320109 0.693004 -0.0802028 -1.579172
[3,] 0.451096 0.693004 0.363799 -0.0421032 -0.829002
[4,] -0.052206 -0.080203 -0.042103 0.0048727 0.095942
[5,] -1.027929 -1.579172 -0.829002 0.0959421 1.889075

Saving covariance matrix in a for loop - R

So there is a hobby project I am currently working on in order to improve my R skills. What I created with my previous code are various subsets of data "returnseries.1, returnseries.2, returnseries.3, ... "(from 1 to 119) which are stored each in a 252x6 matrix.
Now I am building a for loop to calculate the covariance matrix for each subset.
My code goes as the following:
for(k in 1:119){
covmat[k] = matrix(c(cov(returnseries[k])),nrow=6, ncol=6)
}
For some reason I get the error that: "My column index must be at most 7 not 8."
And I don't get why. I tried several other code versions but nothing gives me an answer. Thought that it had to do with the naming but using return series.[k] is providing me an error, that returnseries. is not defined
Would be delighted if somebody could provide a quick
You can use an array. A 3D array in this case.
Generate some data.
> xy <- list(one = matrix(rnorm(9), ncol = 3),
+ two = matrix(rnorm(9), ncol = 3),
+ three = matrix(rnorm(9), ncol = 3))
> xy
$one
[,1] [,2] [,3]
[1,] 0.1341714 -1.27229790 0.22431441
[2,] 1.0853899 0.02335881 -0.05600098
[3,] -1.5645181 0.83745858 -1.47670091
$two
[,1] [,2] [,3]
[1,] 1.4891642 -0.3766222 -0.86981432
[2,] 0.3424295 -1.7882177 1.79601480
[3,] -1.1583058 -0.1604330 0.02690498
$three
[,1] [,2] [,3]
[1,] -0.1511346 -0.3672432 -0.3008405
[2,] -1.9881830 -0.8545396 -0.7108430
[3,] 0.1637134 -0.7958267 1.1923535
Create empty array
> N <- 3
> ar <- array(rep(NA, 3*3*N), dim = c(3, 3, N))
> ar
, , 1
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
[3,] NA NA NA
, , 2
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
[3,] NA NA NA
, , 3
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
[3,] NA NA NA
Fill in values.
> for (i in 1:N) {
+ ar[,, i] <- xy[[i]]
+ }
>
> ar
, , 1
[,1] [,2] [,3]
[1,] 0.1341714 -1.27229790 0.22431441
[2,] 1.0853899 0.02335881 -0.05600098
[3,] -1.5645181 0.83745858 -1.47670091
, , 2
[,1] [,2] [,3]
[1,] 1.4891642 -0.3766222 -0.86981432
[2,] 0.3424295 -1.7882177 1.79601480
[3,] -1.1583058 -0.1604330 0.02690498
, , 3
[,1] [,2] [,3]
[1,] -0.1511346 -0.3672432 -0.3008405
[2,] -1.9881830 -0.8545396 -0.7108430
[3,] 0.1637134 -0.7958267 1.1923535
You can do all sorts of wonderful things with this now. For example, do row sums.
> apply(ar, MARGIN = 3, FUN = rowSums)
[,1] [,2] [,3]
[1,] -0.9138121 0.2427277 -0.8192183
[2,] 1.0527477 0.3502266 -3.5535656
[3,] -2.2037604 -1.2918338 0.5602402
Here's proof for the first matrix. Compare it to the first column:
> rowSums(xy[[1]])
[1] -0.9138121 1.0527477 -2.2037604

Apply function on the rows of a matrix in R

Let's say I have a 5 by 7 matrix and a function f :
a <- matrix(rnorm(7*5),5,7)
f <- function(x,y) sum(x+y)
I would like to compute the matrix b whose element b[i,j] is equal to f(a[i,],a[j,]) without for loops. How could I do ?
You can use outer to apply a function to all possible combinations:
rowNums <- seq(nrow(a)) # vector with all row numbers
outer(rowNums, rowNums, Vectorize(function(x, y) sum(a[x, ] + a[y, ])))
[,1] [,2] [,3] [,4] [,5]
[1,] 6.319860 10.978305 6.911812 2.4609471 4.7021136
[2,] 10.978305 15.636751 11.570257 7.1193924 9.3605589
[3,] 6.911812 11.570257 7.503764 3.0528993 5.2940659
[4,] 2.460947 7.119392 3.052899 -1.3979658 0.8432008
[5,] 4.702114 9.360559 5.294066 0.8432008 3.0843673
Edit:
The calculations are more efficient if you calculate the rowSums before using outer. This code is shorter and faster:
rs <- rowSums(a)
outer(rs, rs, "+")
[,1] [,2] [,3] [,4] [,5]
[1,] 6.319860 10.978305 6.911812 2.4609471 4.7021136
[2,] 10.978305 15.636751 11.570257 7.1193924 9.3605589
[3,] 6.911812 11.570257 7.503764 3.0528993 5.2940659
[4,] 2.460947 7.119392 3.052899 -1.3979658 0.8432008
[5,] 4.702114 9.360559 5.294066 0.8432008 3.0843673
Edit 2:
A solution to your actual problem (see comments):
ta <- t(a) # transpose
apply(a, 1, function(x) colSums(abs(ta - x)))
[,1] [,2] [,3] [,4] [,5]
[1,] 0.000000 10.687579 10.933269 9.306339 7.763612
[2,] 10.687579 0.000000 7.465742 8.517358 7.847622
[3,] 10.933269 7.465742 0.000000 5.768676 6.851272
[4,] 9.306339 8.517358 5.768676 0.000000 6.687477
[5,] 7.763612 7.847622 6.851272 6.687477 0.000000
One way is to use expand.grid to create to subsetting indicies and use on this apply on this:
matrix(apply(expand.grid(seq(nrow(a)),seq(nrow(a))),1,
function(x) f(a[x[1],],a[x[2],])),nrow(a))
[,1] [,2] [,3] [,4] [,5]
[1,] 8.9116431 4.1067161 0.6589584 3.681561 3.207056
[2,] 4.1067161 -0.6982109 -4.1459686 -1.123366 -1.597871
[3,] 0.6589584 -4.1459686 -7.5937263 -4.571123 -5.045629
[4,] 3.6815615 -1.1233656 -4.5711232 -1.548520 -2.023026
[5,] 3.2070558 -1.5978712 -5.0456289 -2.023026 -2.497531

Creating a new matrix in R using old matrix values as exponents

If I have a matrix mat1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
it is possible via a very simple command to square all individual values by
mat1 * mat1
[,1] [,2] [,3]
[1,] 1 9 25
[2,] 4 16 36
Now, what I want to do is to create a new matrix where all values are computed by e^(old_value), e.g., e^1, e^2, e^3 and so forth. How can I do this?
exp computes the exponential function
> mat1 <- matrix(1:6, nrow=2)
> exp(mat1)
[,1] [,2] [,3]
[1,] 2.718282 20.08554 148.4132
[2,] 7.389056 54.59815 403.4288

Resources