Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing a function to calculate the mean of columns in a dataframe in R

Tags:

r

I have to calculate the mean of the columns in a dataframe by writing a function and then applying it. I understand that this is easy to do with mean and apply but I need to write my own function. I have made many attempts but cannot seem to get this right. Below are 3 of my attempts. I am a beginner at R. I would greatly 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,])
}
like image 216
learningcompsci Avatar asked Feb 05 '23 19:02

learningcompsci


2 Answers

Assuming that all the columns in your data frame are numeric, here is a tweak of your first function, where x is a vector (a 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)
like image 174
tatxif Avatar answered Feb 07 '23 08:02

tatxif


Here's an example by slightly modifying your second attempt

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)
}

USAGE

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
like image 37
d.b Avatar answered Feb 07 '23 09:02

d.b