Calculate the Mean For Each Column of a Matrix in R

How to get mean value of each columns in a data frame

In your example x is a matrix. You have two option:

Option 1 - transform x into a data frame and then use sapply

x<-as.data.frame(cbind(x1 = 3, x2 = c(4:1, 2:5)))
x.df<-sapply(x,FUN=mean)

> x.df
x1 x2
3 3

Option 2 - use apply and transform the result in a data frame

x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
x.df<-as.data.frame(t(apply((x),MARGIN=2,FUN=mean)))

> x.df
x1 x2
3 3

Calculating mean of each column in matrix with input value

Maybe this:

#Data
mat <- matrix(1:3,5,6)
#Function
m_col <- function(m, col)
{
mean(m[,col])
}
#Apply
m_col(mat, 2)

Problem about calculating mean in matrix, and convert it into a data frame in R

Are you looking for each matrix row to be a column in the data frame? So the dataframe would be 10 columns and 100 observations each?

This isn't the most efficient way to do this but you could just built out the matrix in the tibble() function. And then you can use the summary() to get descriptive stats for all columns.

library(tidyverse)

df <-
tibble(
sample1 = sd1[1,],
sample2 = sd1[2],
sample3 = sd1[3],
sample4 = sd1[4],
sample5 = sd1[5],
sample6 = sd1[6],
sample7 = sd1[7],
sample8 = sd1[8],
sample9 = sd1[9],
sample10 = sd1[10]
)

summary(df)

Another way to get the means is using pivot_longer() and summarize() as follows

df %>% 
pivot_longer(
cols = c(1:10),
names_to = "sample_type",
values_to = "values"
) %>%
group_by(sample_type) %>%
summarize(sample_means = mean(values))

mean of each matrix of an array in R

1) apply Try apply

apply(m, 3, mean)
## [1] 5.5 5.5 5.5 5.5

2) loop or to do it in a loop:

n <- dim(m)[3]
result <- numeric(n)
for(i in 1:n) result[i] <- mean(m[,,i])
result
## [1] 5.5 5.5 5.5 5.5

3) reshape or reshape it into a matrix and take the column means. n is from above.

colMeans(matrix(m,, n))
## [1] 5.5 5.5 5.5 5.5

Calculate mean of each numeric column and add as result as row

Use rbind and colMeans as in:

> rbind(tbl_mut, colMeans = colMeans(tbl_mut))
timetE4_1 timetE1_2 timetE2_2 timetE3_2 timetE4_2 eve_mean mor_mean tot_mean
1 4048.605 59094.48 27675.59 26374.06 43310.01 7774.442 39113.53 23443.99
2 45729.986 139889.21 111309.64 129781.17 96924.62 43374.117 119476.16 81425.14
3 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
4 4466.153 26250.32 20320.08 18413.54 29061.25 3866.547 23511.30 13688.92
colMeans 173482.724 497479.54 319083.15 330634.05 331434.59 160144.458 369657.83 264901.15

EDIT

Suppose your data frame contains both numeric and non-numeric columns (like the 'Description' column):

> df
Description timetE4_1 timetE1_2 timetE2_2 timetE3_2 timetE4_2 eve_mean mor_mean tot_mean
1 A 4048.605 59094.48 27675.59 26374.06 43310.01 7774.442 39113.53 23443.99
2 B 45729.986 139889.21 111309.64 129781.17 96924.62 43374.117 119476.16 81425.14
3 C 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
4 D 4466.153 26250.32 20320.08 18413.54 29061.25 3866.547 23511.30 13688.92

...then you can use sapply(df, is.numeric) to obtain the numeric columns, on which you then calculate colmeans.

> suppressWarnings(rbind(df, colMeans = colMeans(df[, sapply(df, is.numeric)])))
Description timetE4_1 timetE1_2 timetE2_2 timetE3_2 timetE4_2 eve_mean mor_mean tot_mean
1 A 4048.605 59094.48 27675.59 26374.06 43310.01 7774.442 39113.53 23443.99
2 B 45729.986 139889.21 111309.64 129781.17 96924.62 43374.117 119476.16 81425.14
3 C 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
4 D 4466.153 26250.32 20320.08 18413.54 29061.25 3866.547 23511.30 13688.92
colMeans <NA> 497479.542 319083.15 330634.05 331434.59 160144.46 369657.833 264901.15 173482.72

Or if you know the index of the non-numeric variable, e.g. the first column, you can de-select that column with df[, -1]:

suppressWarnings(rbind(df, colMeans = colMeans(df[, -1]))) 


Related Topics



Leave a reply



Submit