5

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!

3 Answers 3

13

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"])
2
  • 2
    +1, using with makes it clearer in theses cases without too many $s: with(df, mean(val[sta=="A"]))
    – Arun
    Commented Nov 29, 2013 at 9:06
  • 1
    and even more readable (arguable) can be: with(subset(df, sta == "A"), mean(val))
    – flodel
    Commented Nov 29, 2013 at 11:03
3

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.

3

As @PaulHiemstra alluded to, there is a clean data.table solution which would be:

library(data.table)
DT[Status=="A", mean(val)]

where DT <- as.data.table(your_data_frame)


or you can set the key for quicker results:

setkey(DT, "status")
# this will produce a data.table, not a single 
DT["A", mean(val)]
# This produces a single number
DT["A"] [, mean(val)]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.