Element-wise mean in R
how about:
rowMeans(cbind(a, b), na.rm=TRUE)
or
colMeans(rbind(a, b), na.rm=TRUE)
Element-wise mean over list of matrices
You can use:
Reduce("+", my.list) / length(my.list)
According to comments, you want both mean
and sd
implemented on a list of matrices, and the above ways will not work smoothly for sd
. Try this instead :
apply(simplify2array(my.list), 1:2, mean)
apply(simplify2array(my.list), 1:2, sd)
Element wise mean of multiple lists in R
Assuming your A
s are lists of vectors:
Anames <- paste0("A", 1:10)
# for completeness
for(A in Anames)
assign(A, lapply(1:7, function(x) rnorm(1000)))
sapply(1:7, function(i)
{
m <- sapply(Anames, function(A) get(A)[[i]])
mean(m)
})
This avoids building a copy of all your A
s in memory, instead retrieving them one at a time and extracting the desired vector. But if you have enough memory to store all that data, you could probably afford to store a copy as well.
Element wise mean for a list of dataframes with NA
Here is an approach that uses data.table
The steps are (1) coerce each data.frame [element] in x
to data.table, with a column (called rn
) identifying the rownames. (2) on the large data.table, by rowname calculate the mean of each column (with na.rm = TRUE
dealing with NA
values). (3) remove the rn
column
library(data.table)
results <- rbindlist(lapply(x,data.table, keep.rownames = TRUE))[,
lapply(.SD, mean,na.rm = TRUE),by=rn][,rn := NULL]
an alternative would be to coerce to matrix, "simplify" to a 3-dimensional array then apply a mean over the appropriate margins
# for example
results <- as.data.frame(apply(simplify2array(lapply(x, as.matrix)),1:2,mean, na.rm = TRUE))
Sapply function for element-wise calculation a matrix in R
We may use elementwise subtraction of column with outer
outer(data[,1], data[,1], `-`)
If it should be done on each column, loop over the columns (or do asplit
with MARGIN = 2
to split by column), loop over the list
and apply the outer
lapply(asplit(data, 2), function(x) outer(x, x, `-`))
What is the difference between the operator acting elementwise vs on the matrix using Numpy?
Say we've got two matrices:
a = [ p q ]
[ r s ]
b = [ w x ]
[ y z ]
Element-wise product means:
a * b = [ p*w q*x ]
[ r*y s*z ]
Matrix product means:
a @ b = [ (p*w)+(q*y) (p*x)+(q*z) ]
[ (r*w)+(s*y) (r*x)+(s*z) ]
When literature in math, machine learning etc talks about "matrix multiplication", this matrix product is what is meant. Note that a @ b
is not the same as b @ a
.
Related Topics
Extract English Words from a Text in R
R Histogram from Frequency Table
Geom_Bar + Geom_Line: with Different Y-Axis Scale
How to Add a Legend for the Secondary Axis Ggplot
How to Pass Multiple Group_By Arguments and a Dynamic Variable Argument to a Dplyr Function
How to Extract Text from R's Help Command
Arranging Ggally Plots with Gridextra
Terms of a Sum in a R Expression
How Is Ggplot2 Plus Operator Defined
Get Value of Last Non-Na Row Per Column in Data.Table
Cannot Install Library(Xlsx) in R and Look for an Alternative
Changes in Plotting an Xts Object
Shiny: How to Stop Processing Invalidatelater() After Data Was Abtained or at the Given Time