Writing a function to calculate the average number of columns in a dataframe in R

I need to calculate the average of the columns in a dataframe by writing a function and then applying it. I realize this is easy to do with mean

and apply

, but I need to write my own function. I've made many attempts but don't seem to figure it out. Below are three of my attempts. I am new to R. I would really appreciate any suggestions.

mean_fun<-function(x){
  mean_c[i]= sum(x[1:dim(x)],na.rm=TRUE)/length(x[1:dim(x)])
  return(mean_c[i])
}


mean_fun<-function(x){
  for( i in 1:ncol(x)){
    s=sum(x[1:i],na.rm=TRUE)
    l=dim(x[1:i])
    mean_c=s/l
    return (mean_c)
  }


mean_fun<-function(x){
  x=rbind(x,newrow)
  for(i in 1:ncol(x)){
    x[newbottomrownumber,i]=sum[i]/length[i]}
  return(x[1303,])
}

      

+3


source to share


3 answers


Assuming all columns in your dataframe are numeric, here is the setup of your first function where x is a vector (column in mydataframe).



mean_fun<-function(x){
    mean_c= sum(x,na.rm=TRUE)/length(!is.na(x))
    return(mean_c)
}

apply(mydataframe,2,mean_fun)

      

+4


source


Here's an example, slightly modifying your second try

mean_fun<-function(x){
    mean_c = numeric(0)
    for( i in 1:ncol(x)){
        s = sum(x[,i], na.rm=TRUE)
        l = length(x[,i][is.na(x[,i]) == FALSE])
        mean_c[i] = s/l
    }
    return (mean_c)
}

      



USING

mean_fun(mtcars)
# [1]  20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750   0.437500   0.406250
#[10]   3.687500   2.812500

      

+3


source


Why not use it dplyr

?

You can get the average for all columns in your data file.

summarise_each(funs(mean))

      

If we apply it to mtcars

library(dplyr)
mtcars %>% summarise_each(funs(mean))

#       mpg    cyl     disp       hp     drat      wt     qsec     vs      am   gear   carb
#1 20.09062 6.1875 230.7219 146.6875 3.596563 3.21725 17.84875 0.4375 0.40625 3.6875 2.8125

      

0


source







All Articles