0

I have the following data.frame:

df = data.frame(a = sample(c(rep(1,23),rep(2,22), rep(3,43), rep(4, 12))), 
                b = sample(c(rep(1,10),rep(2,10), rep(3,20), rep(4, 60))), 
                c = sample(c(rep(1,40),rep(2,5), rep(3,30), rep(4, 25))))

table(df)

I'd like to run a model on these counts. A model of the following kind:

MCMCglmm(fixed = MyCount ~ a+b , random = c, data=new.df)

My question has to do with how to easily go from df to the new.df (data.frame which contains data expressed the right way). Or how to express 4 variables out of the three first in order to have a count variable of their interaction.

The variable fixed might be define doing MyCount = c(table(df)). But re-expressing a, b and c seem rather complicated to me.

What is the simplest solution? Maybe using the package reshape?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Remi.b
  • 17,389
  • 28
  • 87
  • 168
  • The current code throws errors. Post code that constructs a `new.df` object with the variable/column names – IRTFM Nov 28 '13 at 19:00
  • @Dwin I realized that I fixed the problem but obviously I did not succeeded to make the edit. Sorry about that. It is fixed now. The `new.df` object is the object I aim to construct. I'd like to re-express `df` into count data. Does it make sense? – Remi.b Nov 28 '13 at 19:04

1 Answers1

0

The as.data.frame.table will construct a "Freq" column which I am renaming to 'MyCount';

> new.df <- setNames( as.data.frame(table(df)), c(names(df), "MyCount"))
> str(new.df)
'data.frame':   64 obs. of  4 variables:
 $ a      : Factor w/ 4 levels "1","2","3","4": 1 2 3 4 1 2 3 4 1 2 ...
 $ b      : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 2 2 2 2 3 3 ...
 $ c      : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
 $ MyCount: int  1 2 0 0 1 0 2 2 0 3 ...

BTW, there is no package named "Reshape". Correct capitalization is part of learning R.

IRTFM
  • 258,963
  • 21
  • 364
  • 487