dcast - error: Aggregate function missing

174 Views Asked by At

A little background information regarding my question: I have run a trial with 2 different materials, using 2x2 settings. Each treatment was performed in duplo, resulting in a total number of 2x2x2x2 = 16 runs in my dataset. The dataset has the following headings, in which repetition is either 1 or 2 (as it was performed in duplo).

| Run | Repetition | Material | Air speed | Class. speed | Parameter of interest |

I would like to transform this into a dataframe/table which has the following headings, resulting in 8 columns:

| Run | Material | Air speed | Class. speed | Parameter of interest from repetition 1 | Parameter of interest from repetition 2 |

This means that each treatment (combination of material, setting 1 and setting 2) is only shown once, and the parameter of interest is shown twice.

I have a dataset which looks as follows:

 code     rep material airspeed classifier_speed fine_fraction
1 L17       1 lupine         50             600          1
2 L19       2 lupine         50             600          6
3 L16       1 lupine         60             600          9
4 L22       2 lupine         60             600          12
5 L18       1 lupine         50             1200         4
6 L21       2 lupine         50             1200         6

I have melted it as follows:

melt1 <- melt(duplo_selection, id.vars = c("material", "airspeed", "classifier_speed", "rep"),
          measure.vars=c("fine_fraction"))

and then tried to cast it as follows:

cast <- dcast(melt1, material + airspeed + classifier_speed ~ variable, value.var = "value")

This gives the following message:

Aggregate function missing, defaulting to 'length'

and this dataframe, in which the parameter of interest is counted rather than both values being presented.

1

There are 1 best solutions below

0
On

Thanks for your effort and time to try to help me out, after a little puzzling I found out what I had to do.

I added replicate to each observation, being either a 1 or a 2, as the trial was performed in duplo.

Via the code

cast <- dcast(duplo_selection, material + airspeed + classifier_speed ~ replicate, value.var = "fine_fraction")

I came to the 5 x 8 table I was looking for.