[R] Freq in dataframe

Marc Schwartz marc_schwartz at me.com
Fri Aug 5 15:12:34 CEST 2016


> On Aug 5, 2016, at 4:43 AM, Tripoli Massimiliano <M.Tripoli at aifa.gov.it> wrote:
> 
> Dear R users,
> 
> # This is my data:
> 
> ddat <- array(c(8,10,7,13,8,15,
>              7,9,6,9,7,10,
>               11,13,7,16,8,16
>               ),
>              dim = c(2,3,3),
>               dimnames = list(
>              Time = c("Week 32","Week 52"),
>               Dose = c("Dose 1","Dose 2","Dose 3"),
>             Group = c("A","B","C")))
> ddat
> 
> # and I'd like to have data like that:
> 
> as.data.frame(UCBAdmissions)
> 
> # I tried with 
> 
> as.data.frame(ddat) 
> 
> # and this is the result:
> 
>> as.data.frame(ddat)
>        Dose 1.A Dose 2.A Dose 3.A Dose 1.B Dose 2.B Dose 3.B Dose 1.C Dose 2.C Dose 3.C
> Week 32        8        7        8        7        6        7       11        7        8
> Week 52       10       13       15        9        9       10       13       16       16
> 
> Someone could help me ?
> Thanks in advance,
> 
> M. Tripoli


This has to do with method dispatch.

UCBAdmissions is a table:

> class(UCBAdmissions)
[1] "table"

ddat is an array as you created it:

> class(ddat)
[1] "array"


There is an as.data.frame() method for arrays (as.data.frame.array()) which results in a flattening of the object and this is defined in ?as.data.frame in the Details section:

"Arrays can be converted to data frames. One-dimensional arrays are treated like vectors and two-dimensional arrays like matrices. Arrays with more than two dimensions are converted to matrices by ‘flattening’ all dimensions after the first and creating suitable column labels."

In the case of UCBAdmissions, as.data.frame.table() is called yielding the alternative result that you observe.

If you want to get the same result from ddat, you can either coerce it to a table first:

> as.data.frame(as.table(ddat))
      Time   Dose Group Freq
1  Week 32 Dose 1     A    8
2  Week 52 Dose 1     A   10
3  Week 32 Dose 2     A    7
4  Week 52 Dose 2     A   13
5  Week 32 Dose 3     A    8
6  Week 52 Dose 3     A   15
7  Week 32 Dose 1     B    7
8  Week 52 Dose 1     B    9
9  Week 32 Dose 2     B    6
10 Week 52 Dose 2     B    9
11 Week 32 Dose 3     B    7
12 Week 52 Dose 3     B   10
13 Week 32 Dose 1     C   11
14 Week 52 Dose 1     C   13
15 Week 32 Dose 2     C    7
16 Week 52 Dose 2     C   16
17 Week 32 Dose 3     C    8
18 Week 52 Dose 3     C   16

or explicitly call as.data.frame.table():

> as.data.frame.table(ddat)
      Time   Dose Group Freq
1  Week 32 Dose 1     A    8
2  Week 52 Dose 1     A   10
3  Week 32 Dose 2     A    7
4  Week 52 Dose 2     A   13
5  Week 32 Dose 3     A    8
6  Week 52 Dose 3     A   15
7  Week 32 Dose 1     B    7
8  Week 52 Dose 1     B    9
9  Week 32 Dose 2     B    6
10 Week 52 Dose 2     B    9
11 Week 32 Dose 3     B    7
12 Week 52 Dose 3     B   10
13 Week 32 Dose 1     C   11
14 Week 52 Dose 1     C   13
15 Week 32 Dose 2     C    7
16 Week 52 Dose 2     C   16
17 Week 32 Dose 3     C    8
18 Week 52 Dose 3     C   16

Regards,

Marc Schwartz



More information about the R-help mailing list