Apply function on the rows of a matrix in R - 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

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

simultaneous matrix operations in all list objects in R

I have a list of matrices 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]))
which results in:
[[1]]
[,1] [,2] [,3]
[1,] 1.4570077 -0.33487534 -1.3089918
[2,] -0.3348753 0.07696698 0.3008557
[3,] -1.3089918 0.30085569 1.1760127
[[2]]
[,1] [,2] [,3]
[1,] 5.502298 -1.0065968 -1.1870541
[2,] -1.006597 0.1841480 0.2171611
[3,] -1.187054 0.2171611 0.2560926
[[3]]
[,1] [,2] [,3]
[1,] 0.3303260 0.3141712 0.3244131
[2,] 0.3141712 0.2988064 0.3085474
[3,] 0.3244131 0.3085474 0.3186061
[[4]]
[,1] [,2] [,3]
[1,] 0.7921673 0.4247196 0.8886017
[2,] 0.4247196 0.2277129 0.4764227
[3,] 0.8886017 0.4764227 0.9967755
I want for each list object to sum the columns and find the minimum of these summation. For example min.results[[1]] = min(-0.186,0.042,0.167)=-0.186.
We may use sapply to loop over the list, get the column wise sum (colSums) and return with the minimum
sapply(results, \(x) min(colSums(x)))
-output
[1] -0.1868594 -0.7138005 0.9215250 1.1288552
Or using collapse
library(collapse)
fmin(dapply(results, colSums))
[1] -0.1868594 -0.7138005 0.9215250 1.1288552

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

R, Select a Subset of Matrix

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

Passing more than 2 vectors in R [duplicate]

This question already has answers here:
Trying to pass more than 2 vectors to a function in R
(2 answers)
Closed 5 years ago.
I want to compute e^(ax+b) over a=-1:1 and b=-1:1 for various values of X. I want the output in the form a list of 5 elements. Each element in the list is 3X3 Matrix.
I did achieve this using Outer and Vectorize.
sigm = function(a=0,b=0,x){
return(exp(x*a+b))
}
sigm1 = Vectorize(function(a=-1:1,b=-1:1,x){
outer(a,b,sigm,x)
},SIMPLIFY = FALSE)
Now, sigm1(x=1:3) gives the required output
[[1]]
[,1] [,2] [,3]
[1,] 0.1353353 0.3678794 1.000000
[2,] 0.3678794 1.0000000 2.718282
[3,] 1.0000000 2.7182818 7.389056
[[2]]
[,1] [,2] [,3]
[1,] 0.04978707 0.1353353 0.3678794
[2,] 0.36787944 1.0000000 2.7182818
[3,] 2.71828183 7.3890561 20.0855369
[[3]]
[,1] [,2] [,3]
[1,] 0.01831564 0.04978707 0.1353353
[2,] 0.36787944 1.00000000 2.7182818
[3,] 7.38905610 20.08553692 54.5981500
The only draw back with this code snippet is I am using the default values of a=-1:1 and b=-1:1. When I try to pass the same during function calling, it goes haywire. E.g.
sigm1(-1:1,-1:1,1:3)
[[1]]
[,1]
[1,] 0.1353353
[[2]]
[,1]
[1,] 1
[[3]]
[,1]
[1,] 54.59815
I am unable to figure out why passing the arguments is making this difference in output.
You could use sapply to create your vectorized function
sigm = function(a, b, x){
return(lapply(x, function(xx) sapply(b, function(bb)
sapply(a, function(aa) exp(xx*aa+bb)))))
}
sigm(a = -1:1, b = -1:1, x = 1:3)
#[[1]]
# [,1] [,2] [,3]
#[1,] 0.1353353 0.3678794 1.000000
#[2,] 0.3678794 1.0000000 2.718282
#[3,] 1.0000000 2.7182818 7.389056
#[[2]]
# [,1] [,2] [,3]
#[1,] 0.04978707 0.1353353 0.3678794
#[2,] 0.36787944 1.0000000 2.7182818
#[3,] 2.71828183 7.3890561 20.0855369
#[[3]]
# [,1] [,2] [,3]
#[1,] 0.01831564 0.04978707 0.1353353
#[2,] 0.36787944 1.00000000 2.7182818
#[3,] 7.38905610 20.08553692 54.5981500

Resources