An easier way to get average of a table with some conditions in R -


i trying average of 6 quizzes each male student. here part of code i've tried:

a<-subset(mydf,sex=="m") b<-a[4:9] b sum(b[1:6]) 

my logic table contains male students each of 6 quizzes, sum table , divide number of male student. think there should easier way this.

sample data:

df <- data.frame(section=c(rep('a',9)),                  degree=c(rep('mba',4),'ms','mba','mba','ms','mba'),                  sex=c(rep('m',5),'f','m','m','f'),                  quiz1=c(0,10,2,2,8,6,6,2,3),                  quiz2=c(0,1,4,4,1,5,0,3,9),                  quiz3=c(6,5,6,6,4,2,7,9,3),                  quiz4=c(5,4,5,5,10,5,7,7,3),                  quiz5=c(7,3,6,3,10,7,6,10,5),                  quiz6=c(3,8,6,6,5,8,10,10,5)) 

how this:

data.frame(df[which(df$sex=='m'),],quizmeans=rowmeans(df[which(df$sex=='m'),c(4:9)])) 

note: "c(4:9)" in code above takes row average quiz columns 4-9. we're calculating quiz scores each individual way.

output:

  section degree sex quiz1 quiz2 quiz3 quiz4 quiz5 quiz6 quizmeans 1          mba   m     0     0     6     5     7     3  3.500000 2          mba   m    10     1     5     4     3     8  5.166667 3          mba   m     2     4     6     5     6     6  4.833333 4          mba   m     2     4     6     5     3     6  4.333333 5           ms   m     8     1     4    10    10     5  6.333333 7          mba   m     6     0     7     7     6    10  6.000000 8           ms   m     2     3     9     7    10    10  6.833333 

then if wanted take mean of means (i.e. grand mean), store above "df", use mean() calculate mean of column quizmeans, this:

df <- data.frame(df[which(df$sex=='m'),],quizmeans=rowmeans(df[which(df$sex=='m'),c(4:9)])) mean(df$quizmeans) [1] 5.285714 

if there missing values in data, you'll need add na.rm=true either mean() or rowmeans() function, this:

mean(df$quizmeans, na.rm=true) [1] 5.285714 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -