Quantcast

convert multi dimensional array to list

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

convert multi dimensional array to list

anto.r
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?
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: convert multi dimensional array to list

Michael Weylandt
On Thu, Oct 4, 2012 at 2:54 PM, anto.r <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: convert multi dimensional array to list

anto.r
Hi Michael

thanks! That was the option if I kept it an array. The list format with $ sign since it leaves me feeling that the names are there and can be easily accessed. Why would you rather not use the $ sign?

I use R-Studio and there names can be selected from a drop-down list, I have found it easier but that could be my lack of proper training in R.

Cheers
Anto




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: convert multi dimensional array to list

arun kirshna
Hi,

You can try this:
list2<-lapply(1:dim(my.df.cast)[3],function(i) my.df.cast[,,i,])
 names(list2)<-unlist(dimnames(my.df.cast)[3])
 list2$May
#, , variable = tmax
#
#      name
#year   Alf Que
 # 2011  18  16
 # 2012  18  16

#, , variable = tmin
#
 #     name
#year   Alf Que
 # 2011  12  11
 # 2012  12  11

list2$May[,,"tmax"]
#      name
#year   Alf Que
 # 2011  18  16
 # 2012  18  16


A.K.



----- Original Message -----
From: anto.r <[hidden email]>
To: [hidden email]
Cc:
Sent: Thursday, October 4, 2012 11:57 AM
Subject: Re: [R] convert multi dimensional array to list

Hi Michael

thanks! That was the option if I kept it an array. The list format with $
sign since it leaves me feeling that the names are there and can be easily
accessed. Why would you rather not use the $ sign?

I use R-Studio and there names can be selected from a drop-down list, I have
found it easier but that could be my lack of proper training in R.

Cheers
Anto








--
View this message in context: http://r.789695.n4.nabble.com/convert-multi-dimensional-array-to-list-tp4645011p4645036.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: convert multi dimensional array to list

David Winsemius
In reply to this post by anto.r

On Oct 4, 2012, at 8:57 AM, anto.r wrote:

> Hi Michael
>
> thanks! That was the option if I kept it an array. The list format with $
> sign since it leaves me feeling that the names are there and can be easily
> accessed. Why would you rather not use the $ sign?

It would be better in programing to learn to use the "[[" operator for which '$' is just a particular application that is less flexible because it won't evaluate its argument.

>
> I use R-Studio and there names can be selected from a drop-down list, I have
> found it easier but that could be my lack of proper training in R.

You should be able to do that with column names in dataframes using object[["col"]]

>
> Cheers
> Anto
>
> --
> View this message in context: http://r.789695.n4.nabble.com/convert-multi-dimensional-array-to-list-tp4645011p4645036.html
> Sent from the R help mailing list archive at Nabble.com.

Learn to post context, please. The all too typical habit of Nabble-users in failing to include context is a constant source of annoyance to regular R-help readers.

--

David Winsemius, MD
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: convert multi dimensional array to list

glsnow
See fortune("toad") for a bit more on this concept.

On Thu, Oct 4, 2012 at 3:19 PM, David Winsemius <[hidden email]> wrote:

>
> On Oct 4, 2012, at 8:57 AM, anto.r wrote:
>
>> Hi Michael
>>
>> thanks! That was the option if I kept it an array. The list format with $
>> sign since it leaves me feeling that the names are there and can be easily
>> accessed. Why would you rather not use the $ sign?
>
> It would be better in programing to learn to use the "[[" operator for which '$' is just a particular application that is less flexible because it won't evaluate its argument.
>
>>
>> I use R-Studio and there names can be selected from a drop-down list, I have
>> found it easier but that could be my lack of proper training in R.
>
> You should be able to do that with column names in dataframes using object[["col"]]
>
>>
>> Cheers
>> Anto
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/convert-multi-dimensional-array-to-list-tp4645011p4645036.html
>> Sent from the R help mailing list archive at Nabble.com.
>
> Learn to post context, please. The all too typical habit of Nabble-users in failing to include context is a constant source of annoyance to regular R-help readers.
>
> --
>
> David Winsemius, MD
> Alameda, CA, USA
>
> ______________________________________________
> [hidden email] 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.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[hidden email] 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.
Loading...