Dear all, I have the following list, "aa", composed of two 3*3 tables. I would like to use "apply" function to summarize it, but "apply" cannot handle "list". I want to do it without using any interation. 1. Is there any function like "apply" for list? 2. Is there any way to transform that "list" to a 2*3*3 "array"? thanks cahn ----------------------------------------->aa[[1]] [,1] [,2] [,3] [1,] 1 3 1 [2,] 2 0 1 [3,] 1 1 0 [[2]] [,1] [,2] [,3] [1,] 1 3 1 [2,] 2 0 1 [3,] 1 1 0 I would like to avoid any iteration ------------------------------------------------- Ahn, Chaehyung (cahn) Ph.D. Alpha-Gamma Technologies Inc. My Home: http://blog.naver.com/cahn88
Ahn ChaeHyung wrote:> Dear all, > > I have the following list, "aa", composed of two 3*3 tables. I would like > to use "apply" function to summarize it, but "apply" cannot handle "list". > I want to do it without using any interation. > > 1. Is there any function like "apply" for list? > 2. Is there any way to transform that "list" to a 2*3*3 "array"?Use lapply() or sapply(). Example to transpose both matrices: lapply(a, t) Uwe Ligges> thanks > > cahn > > ----------------------------------------- > >>aa > > [[1]] > [,1] [,2] [,3] > [1,] 1 3 1 > [2,] 2 0 1 > [3,] 1 1 0 > > [[2]] > [,1] [,2] [,3] > [1,] 1 3 1 > [2,] 2 0 1 > [3,] 1 1 0 > > > I would like to avoid any iteration > > > > ------------------------------------------------- > Ahn, Chaehyung (cahn) Ph.D. > Alpha-Gamma Technologies Inc. > My Home: http://blog.naver.com/cahn88 > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
On Mon, 18 Sep 2006, Ahn ChaeHyung wrote:> Dear all, > > I have the following list, "aa", composed of two 3*3 tables. I would like > to use "apply" function to summarize it, but "apply" cannot handle "list". > I want to do it without using any interation. > > 1. Is there any function like "apply" for list??lapply, ?sapply> 2. Is there any way to transform that "list" to a 2*3*3 "array"? >Among other possibilities: library(abind) aa <- list(m1=matrix(rnorm(9), 3, 3), m2=matrix(runif(9), 3, 3)) a1 <- abind(aa, along=3) str(a1) dim(a1)> thanks > > cahn > > ----------------------------------------- > >aa > [[1]] > [,1] [,2] [,3] > [1,] 1 3 1 > [2,] 2 0 1 > [3,] 1 1 0 > > [[2]] > [,1] [,2] [,3] > [1,] 1 3 1 > [2,] 2 0 1 > [3,] 1 1 0 > > > I would like to avoid any iteration > > > > ------------------------------------------------- > Ahn, Chaehyung (cahn) Ph.D. > Alpha-Gamma Technologies Inc. > My Home: http://blog.naver.com/cahn88 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
for lists look at ?lapply() and ?sapply(); for your 2nd question try
something like:
array(unlist(aa), dim = c(3, 3, 2))
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Ahn ChaeHyung" <cahn88 at hotmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, September 19, 2006 3:32 AM
Subject: [R] apply for list or list -> array
> Dear all,
>
> I have the following list, "aa", composed of two 3*3 tables. I
> would like
> to use "apply" function to summarize it, but "apply"
cannot handle
> "list".
> I want to do it without using any interation.
>
> 1. Is there any function like "apply" for list?
> 2. Is there any way to transform that "list" to a 2*3*3
"array"?
>
> thanks
>
> cahn
>
> -----------------------------------------
>>aa
> [[1]]
> [,1] [,2] [,3]
> [1,] 1 3 1
> [2,] 2 0 1
> [3,] 1 1 0
>
> [[2]]
> [,1] [,2] [,3]
> [1,] 1 3 1
> [2,] 2 0 1
> [3,] 1 1 0
>
>
> I would like to avoid any iteration
>
>
>
> -------------------------------------------------
> Ahn, Chaehyung (cahn) Ph.D.
> Alpha-Gamma Technologies Inc.
> My Home: http://blog.naver.com/cahn88
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm