[R] Converting data frame into multidimensional array

Luk Arbuckle luk.arbuckle at gmail.com
Sat Oct 22 02:30:37 CEST 2011


Thank-you for the replies.  I would say that the function xtabs() is
the simplest method. And thanks for pointing out that a table is also
an array, as I clearly wasn't aware of that.

On 2011-10-21, at 19:07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:

> On Fri, Oct 21, 2011 at 7:04 PM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
>> On Fri, Oct 21, 2011 at 4:15 PM, Luk Arbuckle <luk.arbuckle at gmail.com> wrote:
>>> Consider the following data frame
>>>
>>> X <- data.frame(Titanic)
>>>
>>> Does anyone know of an easy way to convert X into a multidimensional
>>> array?  Example that doesn't work
>>>
>>> X <- as.array(X, dim=c(4,2,2,2))
>>>
>>> To do what I need, X needs to be converted into an array of dimensions
>>> c(4,2,2,2) in this case, not a table.
>>>
>>
>> Try this:
>>
>> xtabs(Freq ~., X)
>>
>
> By the way, you asked for an array rather than a table but a table is an array:
>
> is.array(xtabs(Freq ~., X)) # TRUE
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com



More information about the R-help mailing list