I have a dataset which has four columns of data.
I want to group the rows by two variables, and the columns by one variable
Here is an example of my data
df <- data.frame(
Price = rnorm(24),
Grouping = rep(c("CD", "NW", "SMK", "ghd"),6),
Sub_grouping = c("CDapple", "NWapple", "SMKapple", "ghdapple",
"CDPear", "NWpear", "SMKpear", "ghdpear",
"CDgrape", "NWgrape", "SMKgrape", "ghdgrape",
"CDapple", "NWapple", "SMKapple", "ghdapple",
"CDPear", "NWpear", "SMKpear", "ghdpear",
"CDgrape", "NWgrape", "SMKgrape", "ghdgrape"),
SP = rep(c("SP", "OffSP"),12))
To get the average of the Price variable for each subgroup, I can run the following:
df <- melt(df)
df_mean <- dcast(df, Grouping + Sub_grouping ~ SP, value.var = "value", fun.aggregate = mean)
I would also like the average value of Price for each grouping variable. Is this possible?
I also would like to count the number of values for Price which feed into each average Price. So for each Group, by SP and OffSP, the number of prices which feed into this; and for each sub_group, by SP and OffSP, the number of prices which feed into this.
Does anyone know how to do this?
I've seen these questions Create a column with a count of occurrences in R How can I count the number of instances a value occurs within a subgroup in R? But their contingency tables are 2x2, and I need a table with grouping and sub-group as rows, with SP / OffSP as columns.
Thanks