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. Thanks in advance.
David L Carlson
2011-Oct-21 21:31 UTC
[R] Converting data frame into multidimensional array
How about?> x <- array(Titanic, dim=c(4,2,2,2)) > str(x>num [1:4, 1:2, 1:2, 1:2] 0 0 35 0 0 0 17 0 118 154 ... ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Luk Arbuckle Sent: Friday, October 21, 2011 3:16 PM To: r-help at r-project.org Subject: [R] Converting data frame into multidimensional array 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. Thanks in advance. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Gabor Grothendieck
2011-Oct-21 23:04 UTC
[R] Converting data frame into multidimensional array
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) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Apparently Analagous Threads
- How to apply a function to a multidimensional array based on its indices
- Function to compute multi-response, multi-rater kappa?
- Multidimensional array with R
- multiplying multidimensional arrays (was: Re: [R] Manipulation involving arrays)
- [RFC] A new multidimensional array indexing intrinsic