0

My data looks as follows:

 RNR RoundNR   X     Y     Z       Etc. 
 1       1     13    19    0.0          
 1       2     23    23    0.0      
 2       1     34    17    0.3 
 2       2     33    20    0.4

I wish to format this so that for every variable the RoundNR is added to the column header (in practice duplicating every column heading and add 1 or 2 depending on RoundNR, while at the same time halving the number of rows).

I already (successfully) did this via:

temp <- dcast(melt(df_mcmc, id.vars=c("RNR", "RoundNR")), RNR~variable+RoundNR)

And then selected all columns I wanted to create a new df.

However, I restarted my computer and suddenly it throws an error: "Aggregation function missing: defaulting to length".

I figured this might be due to some package error, so I added reshape2:: in front of the entire code and the melt command (I tried all variations of reshape and reshape 2 and infront of dcast and melt).

I get that it wants me to add an aggregation function but it's not supposed to aggregate anything perse (no min,max,etc.). I just want to move some data around. Can anyone explain why this error occurs and how to stop it?

EDIT: The answer provided by akrun helped me find my problem! While adding rowid to the code did not resolve the issue (As mentioned in a comment below) it did help me find the problem. As akrun stated, I need a unique identifier per row. Due to me editing the dataframe for some unrelated task I ended up duplicating one of the rows by accident. As I no longer had a unique ID the code stopped working.

Thank you!

leonheess
  • 16,068
  • 14
  • 77
  • 112
R. Iersel
  • 89
  • 9
  • 4
    It is not an error, but a warning and that shows up when you have duplicate elements and not have a unique identifier. Create a sequence by group and it should work fine i.e. something like `dcast(melt(df_mcmc, id.vars=c("RNR", "RoundNR")), rowid(RNR) + RNR~variable+RoundNR)` – akrun Jul 09 '19 at 13:02
  • Thank you! You are right it's a warning and not an error. I tried your code however this produces a table in which an extra column is added (rowid), and for all rowid's (1 through 69) and additional line is created per RNR resulting in a huge df. Moreover, this df is largely filled with NA's (I don't fully comprehend what it is doing but it's not what I'm looking for). I'd like to add that I realize that I need a unique id but I already have this as each RNR+RoundNR combination only occurs once. Additionally, my old code (mentioned above) used to work fine, which is the most confusing part. – R. Iersel Jul 09 '19 at 13:56

0 Answers0