I have a problem performing a fairly simple ddply operation: I have the following dataframe.
+----------+----------+
| Expenses | Category |
+----------+----------+
| 735 | 1 |
| 992 | 2 |
| 943 | 1 |
| 995 | 3 |
| 914 | 3 |
| 935 | 1 |
| 956 | 3 |
| 946 | 2 |
| 978 | 1 |
| 924 | 1 |
+----------+----------+
I am trying to calculate the N and mean of expenses for each category, by executing the following:
ddply(df, .(Category), summarise, N = length(df$Expenses), mean = mean(df$Expenses))
However i get:
Category N mean
1 1 10 931.8
2 2 10 931.8
3 3 10 931.8
Could you help figuring out what I'm doing wrong here?
Here is the df's dput
:
structure(list(Expenses = c(735, 992, 943, 995, 914, 935, 956,
946, 978, 924), Category = c(1L, 2L, 1L, 3L, 3L, 1L, 3L, 2L,
1L, 1L)), .Names = c("Expenses", "Category"), class = "data.frame", row.names = c(NA,
-10L))