Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Average of values in columns in dataframe?

Tags:

dataframe

r

mean

I want to find the mean across a dataframe of values. For example, if I have the following data:

ID Value Status
1   10     A
2   15     B
3   20     A

And I want to find the mean of all values with the status A in it. How would I do so?

Here is my attempt:

dataframe$balance.mean(dataframe$status == 'A')

But I keep getting an error that says Error: attempt to apply non-function. Can anyone help me out? Thanks!

like image 352
user1871869 Avatar asked Nov 29 '13 06:11

user1871869


2 Answers

If I understood your requirement clearly, following should meet your requirement:

 id<-c(1,2,3)
 val<-c(10,15,20)
 sta<-c("A","B","A")

 df<-data.frame(id,val,sta)

 mean(df$val[df$sta=="A"])
like image 99
rags Avatar answered Oct 23 '22 22:10

rags


Remember that () is used for function calls, [] are used for subsetting. Your are now calling a function while there is actually no function, giving the error message you see.

In a more general sense, for these kinds of things I like to use plyr, although data.table is an awesome other option.

library(plyr)
ddply(dataframe, .(Status), summarize, mean_value = mean(Value))

This will yield you a new data.frame with the average values of Value for each unique value of Status.

like image 36
Paul Hiemstra Avatar answered Oct 23 '22 21:10

Paul Hiemstra