Skip to content
Back to formatted view

Raw Message

Message-ID: <CAAmySGMPeoRei=C-uMFvw3pDJo_n7hvHGUQK3nCmBv4DAxNmow@mail.gmail.com>
Date: 2012-10-04T15:25:06Z
From: R. Michael Weylandt
Subject: convert multi dimensional array to list
In-Reply-To: <1349358869531-4645011.post@n4.nabble.com>

On Thu, Oct 4, 2012 at 2:54 PM, anto.r <anto.rajaa at gmail.com> wrote:
> I have an 4D named array (the result of recast() on a dataframe) and would
> like to convert it to a list with the names retained.
>
> Example:
>>my.df<-expand.grid(name=c("Alf","Que"), month=c("May","Jun"),
> year=c("2011","2012"))
>>my.df$tmax<-sample(15:20,4)
>>my.df$tmin<-sample(10:15,4)
>>my.df.melt=melt(my.df)
>>my.df.cast=cast(my.df.melt, year~name~month~variable)
>
> I want to convert it to a list so I can access the elements by name like
>>my.df.cast$tmax$May
> to get the corresponding year-name matrix
>
> This assuming, lists provide an easier way to handle this data. Does it?

I might actually stick with an array, but avoid the dollar sign syntax:

my.df.cast[,,"May",]

would be one way.

Cheers,
M


> Also, may be there is a way to get the list I want from my.df without going
> through the melt and cast.  Any suggestions are welcome.
>
> Thanks in advance
> Anto
>
> PS: I came across threads for converting list to array but not the other way
> round.
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/convert-multi-dimensional-array-to-list-tp4645011.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.