0
votes

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
(1) Please don't paste data as an image, I'm not going to spend time transcribing it, especially when you have it in a convenient textual format on your console. It it much better to include sample data in your question by pasting the output from dput(x), where x is the sample (perhaps head(myframe)). (2) Is this data.table::dcast or reshape2::dcast? - r2evans
(3) The message about defaulting to 'length' is not an error, it is just a message telling you that you did not provide an aggregating function. If you read ?dcast (from whichever package), it mentions fun.aggregate=. - r2evans
I used reshape2::dcast My apologies for the picture, I will upload the data as a table in my question - Marjanne
It can be difficult to produce a good, reproducible question, and taking shortcuts frequently breaks things. In this case, Error: id variables not found in data: rep. There's an R package reprex that helps with this process, by ensuring that code you intend to post here does not error for (accidental, avoidable) reasons like this. - r2evans
We are still in the position that this is not an error, nor is it necessarily wrong. It is just informing you that widening/reshaping with dcast can result in data aggregation/combination, and you have not been explicit about how you want that done. You've not mentioned if the results from dcast(.) are correct; if they are, then that message goes away by adding dcast(., fun.aggregate=length). If not, then please edit your question and add what you expect the results to be. - r2evans

1 Answers

0
votes

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.