user330
user330

Reputation: 1270

How to calculate mean and Sd for multiple data frames in R

I have two data frames as follows,

df1
    temp    time    mot
    12  13  12
    12  13  12
    12  13  12
df2
    temp    time    mot
    7   4   2
    7   4   2
    7   4   5

Both dummy tables and please assume I have multiple columns

The logit is to calculate mean and Sd for each column to get the following table


    Var1    Mean1   SD1 Var2    Mean2   SD2
    temp1   12  0   temp2   7   0
    time1   13  0   time2   4   4
    mot1    12  0   mot2    3   1.732050808


I get stuck to do it for multiple data frames with multiple columns.

Upvotes: 0

Views: 537

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388797

You can put the dataframes in a list and get column-wise mean and sd for each data.

list_df <- list(df1, df2)
result <- do.call(cbind,lapply(list_df, function(x) 
             data.frame(col = names(x), mean = colMeans(x), sd = sapply(x, sd))))
result

#      col mean sd  col mean    sd
#temp temp   12  0 temp    7 0.000
#time time   13  0 time    4 0.000
#mot   mot   12  0  mot    3 1.732

If you want column names of result to be unique you can use make.unique

names(result) <- make.unique(names(result))

Upvotes: 1

Duck
Duck

Reputation: 39585

Try this:

library(dplyr)
library(tidyr)
#Code
new <- df1 %>% bind_rows(df2,.id = 'id') %>%
  pivot_longer(-id) %>%
  mutate(Var=paste0(name,id)) %>%
  group_by(Var,.drop = F) %>%
  summarise(Mean=mean(value,na.rm = T),
            SD=sd(value,na.rm = T))
#List
List <- split(new,gsub('[a-z]','',new$Var))
List <- lapply(1:length(List), function(x) {names(List[[x]])<-paste0(names(List[[x]]),x);List[[x]]})
#Bind
res <- do.call(bind_cols,List)

Output:

# A tibble: 3 x 6
  Var1  Mean1   SD1 Var2  Mean2   SD2
  <chr> <dbl> <dbl> <chr> <dbl> <dbl>
1 mot1     12     0 mot2      3  1.73
2 temp1    12     0 temp2     7  0   
3 time1    13     0 time2     4  0   

Upvotes: 1

Related Questions