简体   繁体   中英

Find the mean and standard deviation of all values in a data frame

I have a set of data frames each with different column names, eg frameOne looks like

   Q2  Q6  Q9
1   1   0   0
2   0   1   1
...
N   1   1   0

and frameTwo is

   Q1  Q5  Q9 Q22
1   1   1   0   1
2   1   0   1   0
...
N   1   1   1   0

How would I calculate the mean and standard deviation of the entire frame without explicitly stating the column names?

Based on your answer, I'm guessing you're after this.

df1 <- as.data.frame(matrix(runif(9), ncol = 3))
df2 <- as.data.frame(matrix(runif(9), ncol = 3))
df3 <- as.data.frame(matrix(runif(9), ncol = 3))
df4 <- as.data.frame(matrix(runif(9), ncol = 3))

my.objs <- ls(pattern = "df")

sapply(my.objs, FUN = function(x) {
  st <- as.vector(as.matrix(get(x)))
  data.frame(mean = mean(st), sd = sd(st))
})

     df1       df2       df3       df4      
mean 0.4967452 0.4426861 0.5198141 0.3460732
sd   0.2533854 0.2179547 0.3106693 0.3179838

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM