Meaning of %o% in R - r

I encountered the following in R:
x=x+y%o%c(1.5,1.5)
I am wondering what is the meaning of %o% here. I tried googling but didn't have much luck

There are a number of shortcuts in R that use the %...% notation. %o% is the outer product of arrays
> 1:3 %o% 1:3
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 4 6
[3,] 3 6 9
There are a number of others, my most used is %in%:
3 %in% c(1,2,3,4) #TRUE
5 %in% c(1,2,3,4) #FALSE
3.4 %in% c(1,2,3,4) #FALSE
There are a few others, I don't know them all off the top of my head. But when you encounter them, you can check for documentation by using backticks around the %o% like ?`%o%`, or quotes ?'%o%' (or ?"%o%").
They are obviously difficult to google because of the percent sign.

An intuition. %o% is outer product, look at the example, it returns a matrix. a[1] * b is the first row of matrix, a[2] * b is the second row of the matrix.
> a = c(1, 2, 3)
> b = c(0, 2, 4)
> a %o% b
[,1] [,2] [,3]
[1,] 0 2 4
[2,] 0 4 8
[3,] 0 6 12

Related

trouble rearranging my 2x2 matrix in a simple way in R

I'm trying to turn
df<-matrix(1:4,nrow = 2,ncol = 2)
df
[,1] [,2]
[1,] 1 3
[2,] 2 4
into
matrix(c(2,4,1,3),nrow = 1,ncol = 4)
2 4 1 3
so that i can run it through a for loop to rbind many entries.
I've been trying
cbind(df[row 2,],df[row 1,])
but it's not working. Is there a simple way to do this that won't require me to separate the matrix and then bring it back together?
Here is another way. Without the call to matrix it returns a vector, not a matrix.
df <- matrix(1:4, 2)
matrix(c(t(df[nrow(df):1,])), 1)
# [,1] [,2] [,3] [,4]
#[1,] 2 4 1 3
We can use
t(c(t(df[nrow(df):1, ])))
# [,1] [,2] [,3] [,4]
#[1,] 2 4 1 3
Turning a comment into an answer, a fourth option is
rev(t(m[, ncol(m):1]))
# [1] 2 4 1 3
with
m <- matrix(1:4, 2)
Maybe you can try the code below
r <- unlist(rev(data.frame(t(df))))
or
r <- do.call(c,rev(split(df,1:nrow(df))))
or
r <- unlist(rev(split(df,1:nrow(df))))

How to apply a function on every element of all elements in a list in R

I have a list containing matrices of the same size in R. I would like to apply a function over the same element of all matrices. Example:
> a <- matrix(1:4, ncol = 2)
> b <- matrix(5:8, ncol = 2)
> c <- list(a,b)
> c
[[1]]
[,1] [,2]
[1,] 1 3
[2,] 2 4
[[2]]
[,1] [,2]
[1,] 5 7
[2,] 6 8
Now I want to apply the mean function and would like to get a matrix like that:
[,1] [,2]
[1,] 3 5
[2,] 4 6
One conceptual way to do this would be to sum up the matrices and then take the average value of each entry. Try using Reduce:
Reduce('+', c) / length(c)
Output:
[,1] [,2]
[1,] 3 5
[2,] 4 6
Demo here:
Rextester
Another option is to construct an array and then use apply.
step 1: constructing the array.
Using the abind library and do.call, you can do this:
library(abind)
myArray <- do.call(function(...) abind(..., along=3), c)
Using base R, you can strip out the structure and then rebuild it like this:
myArray <- array(unlist(c), dim=c(dim(a), length(c)))
In both instances, these return the desired array
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
step 2: use apply to calculate the mean along the first and second dimensions.
apply(myArray, 1:2, mean)
[,1] [,2]
[1,] 3 5
[2,] 4 6
This will be more flexible than Reduce, since you can swap out many more functions, but it will be slower for this particular application.

Strange behavior of apply/ reverse function in R

I have a simple matrix:
mat = rbind(c(1:3),c(4:6),c(7:9))
mat
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 4 5 6
# [3,] 7 8 9
I want to now reverse the matrix row-wise. That is I want to obtain:
revMat
# [,1] [,2] [,3]
# [1,] 3 2 1
# [2,] 6 5 4
# [3,] 9 8 7
To do this I tried
apply(mat, 1, rev)
And the result was:
# [,1] [,2] [,3]
# [1,] 3 6 9
# [2,] 2 5 8
# [3,] 1 4 7
I find this to be extremely strange. It's like the rows are reversed and then the final matrix is transposed. I don't understand why. If I try simply, for instance,
apply(mat, 2, rev)
it gives me the expected reversal of each column
# [,1] [,2] [,3]
# [1,] 7 8 9
# [2,] 4 5 6
# [3,] 1 2 3
Therefore to obtain the final result I have to perform
t(apply(t(bg), 2, rev))
Thus obtaining the required matrix is NOT a problem for me, but I don't understand the "anomaly" in the behavior of apply/ reverse. Can anyone explain this to me?
Edit: To make clear the distinction, I already know how to do the reversal. I want to know WHY this happens. How to is clear from many earlier questions including
How to reverse a matrix in R?
apply always puts the result in the first dimension. See ?apply for more information. Assuming this input:
mat <- matrix(1:9, 3, byrow = TRUE)
here are some alternatives:
1) transpose
t(apply(mat, 1, rev))
2) avoid apply with indexing
mat[, 3:1]
3) iapply An idempotent apply was posted here:
https://stat.ethz.ch/pipermail/r-help/2006-January/086064.html
Using that we have:
iapply(mat, 1, rev)
There was also an idempotent apply, iapply, in version 0.8.0 of the reshape package (but not in the latest version of reshape): https://cran.r-project.org/src/contrib/Archive/reshape/
4) rollapply rollapply in the zoo package can be used:
library(zoo)
rollapply(mat, 1, rev, by.column = FALSE)
5) tapply The tapply expression here returns a list giving us the opportunity to put it together in the way we want -- in this case using rbind:
do.call("rbind", tapply(mat, row(mat), rev))
6) multiply by a reverse diagonal matrix Since rev is a linear operator it can be represented by a matrix:
mat %*% apply(diag(3), 1, rev)
or
mat %*% (row(mat) + col(mat) == 3+1)
If you look at the help for apply(), this is exactly the behavior you would expect:
Value
If each call to FUN returns a vector of length n, then apply returns
an array of dimension c(n, dim(X)[MARGIN]) if n > 1.
a nice option to do what you want is to use indexing:
mat[,ncol(mat):1]

Loop Matrix to store in Cube

I want to loop an equation through a matrix and store the results in a cube, so that Cube[,,1] is one result of the matrix.
I currently have written the following
PercentileReturn <- array(NA, c(RetAge,length(Percentile)+1,nrow(Demo)))
for (i in 1:nrow(Demo)) {
PercentileReturn[,,i] <-
PercentileReturn[Demo[i,3]:RetAge,
1:length(Percentile),1]<-
t(apply((apply(AnnualReturns[(Demo[i,3]):RetAge,],2,cumprod)) *
Demo[i,4],1,function(x){quantile(x, Percentile, na.rm=T)}))
}
and it results in the following error
Error in PercentileReturn[, , i] <- PercentileReturn[Demo[i, 3]:RetAge, :
number of items to replace is not a multiple of replacement length
I assume it's because the Matrix I am trying to plug in isn't in 3 dimensions.
Basically a stripped down version would be to have an
array(NA,c(2,2,3)) populated with a matrix times a vector
so that say
Matrix * vector c(1,2,3)
[,1] [,2]
[1,] 4 4
[2,] 4 4
would result in the following cube
, , 1
[,1] [,2]
[1,] 4 4
[2,] 4 4
, , 2
[,1] [,2]
[1,] 8 8
[2,] 8 8
, , 3
[,1] [,2]
[1,] 12 12
[2,] 12 12
That will do it:
M <- matrix(1:4,2) # in your example M <- matrix(4, 2,2)
x <- 1:3
array(sapply(x, function(xi) M*xi), c(dim(M), length(x)))
I found the error the first
PercentileReturn[,,i]
has to also match the dimensions of the loop data below written as
PercentileReturn[Demo[i,3]:RetAge,1:length(Percentile),i]
Thanks Jogo, I will be using something similar to what you wrote in another issue down the line.

Multiply vector by matrix in R should return vector

In R, I want to multiply a 1x3 vector by a 3x3 matrix to produce a 1x3 vector. However R returns a matrix:
> v = c(1,1,0)
> m = matrix(c(1,2,1,3,1,1,2,2,1),nrow=3,ncol=3,byrow=T)
> v*m
[,1] [,2] [,3]
[1,] 1 2 1
[2,] 3 1 1
[3,] 0 0 0
The correct output should be a vector, not a matrix
If in doubt, try the help system, here eg help("*") or help("Arithmetic"). You simply used the wrong operator.
R> v <- c(1,1,0)
R> m <- matrix(c(1,2,1,3,1,1,2,2,1),nrow=3,ncol=3,byrow=T)
R> dim(m)
[1] 3 3
R> dim(v)
NULL
R> dim(as.vector(v))
NULL
R> dim(as.matrix(v, ncol=1))
[1] 3 1
R>
R> m %*% as.matrix(v, ncol=1)
[,1]
[1,] 3
[2,] 4
[3,] 4
R>
Note that we have to turn v into a proper vector first. You did not say whether it was 1x3 or 3x1. But luckily R is generous:
R> v %*% m
[,1] [,2] [,3]
[1,] 4 3 2
R> m %*% v
[,1]
[1,] 3
[2,] 4
[3,] 4
R>
Useful functions in this case are crossprod and tcrossprod
> tcrossprod(v, m)
[,1] [,2] [,3]
[1,] 3 4 4
See ?crossprod and ?tcrossprod for details.
Are you looking for
as.vector(v %*% m)
?
Here the documentation of matmult:
Multiplies two matrices, if they are conformable. If one argument
is a vector, it will be promoted to either a row or column matrix
to make the two arguments conformable. If both are vectors it
will return the inner product (as a matrix).

Resources