Average a list of matrix [duplicate] - r

This question already has answers here:
Element-wise mean over list of matrices [duplicate]
(2 answers)
Closed 6 years ago.
For example, I have a list of matrix like this
list2<-lapply(1:2, function(x) matrix(rnorm(6, 10, 1), nrow=2, ncol=3))
list2
How do I get a matrix which has the same size with each matrix in each list, and the value in each cell equal to the average of corresponding cell across lists.

We can do this by adding the corresponding cells in each list elements and divide by the length of the list
Reduce(`+`, list2)/length(list2)
Or another option is to unlist the list, create a 3D array, use apply to get the mean
apply(array(unlist(list2), c(2,3,2)), c(1,2), mean)

Related

Replace value in dataframe by value divided by mean of the entire column [duplicate]

This question already has answers here:
Normalizing selection of dataframe columns with dplyr
(2 answers)
Closed 3 years ago.
In need to normalise my data by dividing each value by the mean of the entire column, preferably using dplyr.
assume
inputs <- c(3,5,3,9,12)
mydata = data.frame(inputs)
I would like all the values replaced by themselves divided by the mean, which is 6.4.
Any straightforward suggestion?
We can use sapply in base R for generalized approach
sapply(mydata, function(x) x/mean(x))
Or with colMeans if more than one column
mydata/colMeans(mydata)[col(mydata)]

Reshaping matrix into vector of alternate columns [duplicate]

This question already has answers here:
Convert a matrix to a 1 dimensional array
(11 answers)
Closed 4 years ago.
I have a matrix measuring 91 x 2 (i.e 91 rows and two columns).
mat1 <- matrix(1:182, 91, 2)
I need to create a vector from the said matrix of one row. I can do that with the following:
mat2 <- matrix(mat1, nrow = 1, byrow = TRUE).
However, I would like to have each row in the original matrix to be represented one after another. Currently it's taking all of column 1 then all of column 2 and joining those together sequentially. Whilst I need them to be in one long row, like this: 1,92,2,93,3,94 etcMeaning the structure ultimately would be 1,182 (i.e. one row with 182 columns).
How can I achieve this?
Thanks.
We can transpose the matrix and convert it to a vector
c(t(mat1))

how to get lengths of data frames [duplicate]

This question already has answers here:
number of rows each data frame in a list [duplicate]
(2 answers)
How to count rows?
(3 answers)
Closed 5 years ago.
I have a list of data frames with different lengths. How do I get the length of each data frame? I tried nrow function but it returns "NULL".
The lengths is for getting the length of list of vectors. Here, we need the nrow or ncol
sapply(lst, nrow)
sapply(lst, ncol)
depending upon what the OP considers as length

R - sum vectors matching names [duplicate]

This question already has answers here:
Sum rows in data.frame or matrix
(7 answers)
Closed 7 years ago.
I need to sum columns of a table that have a names starting with a particular string.
An example table might be:
tbl<-data.frame(num1=c(3,2,9), num2=c(3,2,9),n3=c(3,2,9),char1=c('a', 'b', 'c'))
I get the list of columns (in this example I wrote only 2, but the real case has more tan 20).
a<-colnames(tbl)[grep('num', colnames(tbl))]
I tried with
sum(tbl[,a])
But I get only one number with the total sum of the elements in both vectors.
What I need is the result of:
tbl$num1+ tbl$num2
We can either use Reduce
Reduce(`+`, tbl[a])
Or rowSums. The rowSums also has the option of removing the NA elements with na.rm=TRUE.
rowSums(tbl[a])

How can I calculate mean over a list of 2D matrices? [duplicate]

This question already has answers here:
Mean of each element of a list of matrices
(3 answers)
Closed 7 years ago.
I have a list of 2D matrices which I want to calculate some statistics on it, for example calculate the mean. I can convert the list to array and use apply like:
dat1<-list(a = matrix(seq(1,4),2,2), b= matrix(seq(5,8),2,2), c= matrix(seq(9,12),2,2))
# convert to array
obs <-array(unlist(dat1), dim = c(2, 2, 3))
meanObs <- apply(obs, c(1,2), mean, na.rm = TRUE)
Is there anyway to perform this without converting the list to array?!! Calculating the mean directly from list.
Thanks so much!
Since it's the mean you want, I think you can do
Reduce("+",dat1)/length(dat1)
Reduce() takes the sum of the matrices, element by element, in pairs. Then you just have to divide by N.
This could work for other statistics (e.g. Reduce(pmax,dat1) should give you the elementwise maxima) but would be harder if you were trying to compute some other summary statistic such as the median ... in that case I don't think you could avoid looping (implicitly or explicitly) over the elements of the matrices.

Resources