Re: [R] list to matrix
Hi Do not post HTML. Why you did not populate your list directly with coefficients by let say coef(lm.result)? Anyway, you can reveal structure of individulal list component by str(your.object[[1]]). After that you can extract coefficient component and use sapply/lapply probably with rbind. maybe sapply(lapply(your.list, coef), rbind) can do it. Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of eliza botto Sent: Wednesday, September 11, 2013 4:23 PM To: r-help@r-project.org Subject: [R] list to matrix Dear useRs, If i have a list of the following form and i want to convert the coefficient section of each element, combined into one matrix of dimension 3*5. How can i do that?I hope i am clear thank in advance [[1]] Call: lm(formula = mm[, i] ~ 0 + (x0 + x + xx + y + yy)) Coefficients: x0x xxy yy 1. -0.4250 0.2494 0.1683 -0.7449 [[2]] Call: lm(formula = mm[, i] ~ 0 + (x0 + x + xx + y + yy)) Coefficients: x0x xxy yy 1. -0.6355 0.5876 0.2518 -0.7293 [[3]] Call: lm(formula = mm[, i] ~ 0 + (x0 + x + xx + y + yy)) Coefficients: x0x xxy yy 1. 0.5778 0.3838 0.4207 -0.1354 [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] list to matrix
Hi, Try: set.seed(48) lst1-replicate(3,data.frame(y=rnorm(50),z=runif(50),x=sample(10:15,50,replace=TRUE)),simplify=FALSE) t(sapply(lst1,function(u) coef(lm(y~0+x+z,data=u #change accordingly # x z #[1,] -0.01020553 0.3852990 #[2,] -0.01157726 0.3986898 #[3,] 0.01788307 -0.5624307 A.K. - Original Message - From: eliza botto eliza_bo...@hotmail.com To: r-help@r-project.org r-help@r-project.org Cc: Sent: Wednesday, September 11, 2013 10:22 AM Subject: [R] list to matrix Dear useRs, If i have a list of the following form and i want to convert the coefficient section of each element, combined into one matrix of dimension 3*5. How can i do that?I hope i am clear thank in advance [[1]] Call: lm(formula = mm[, i] ~ 0 + (x0 + x + xx + y + yy)) Coefficients: x0 x xx y yy 1. -0.4250 0.2494 0.1683 -0.7449 [[2]] Call: lm(formula = mm[, i] ~ 0 + (x0 + x + xx + y + yy)) Coefficients: x0 x xx y yy 1. -0.6355 0.5876 0.2518 -0.7293 [[3]] Call: lm(formula = mm[, i] ~ 0 + (x0 + x + xx + y + yy)) Coefficients: x0 x xx y yy 1. 0.5778 0.3838 0.4207 -0.1354 [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] list to matrix?
On Wed, Dec 5, 2012 at 12:02 AM, arun smartpink...@yahoo.com wrote: Hi, p - lapply(1:1e6, function(i)c(i, log2(i))) system.time(z1 - t(sapply(p,function(x)x))) # user system elapsed # 2.568 0.048 2.619 system.time(z1 - do.call(rbind,p)) # user system elapsed # 4.000 0.052 4.060 A.K. Thanks for that Arun -- I'll have to look into why rbind is so slow. Some interesting notes: 1) On my machine t(sapply(p, function(x) x)) is about 2x faster than t(sapply(p, identity)) 2) Similarly, do.call(rbind, p) is about 4x slower than do.call(cbind, p) which all goes to show, profiling is all-important (as Bill reminds me often) and often counter-intuitive. Michael __ R-help@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.
[R] list to matrix?
How do I convert a list to a matrix? --8---cut here---start-8--- list(c(5, 101), c(1e+05, 46), c(15, 31), c(2e+05, 17), c(25, 19), c(3e+05, 11), c(35, 12), c(4e+05, 25), c(45, 19), c(5e+05, 16)) as.matrix(a) [,1] [1,] Numeric,2 [2,] Numeric,2 [3,] Numeric,2 [4,] Numeric,2 [5,] Numeric,2 [6,] Numeric,2 [7,] Numeric,2 [8,] Numeric,2 [9,] Numeric,2 --8---cut here---end---8--- thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://dhimmi.com http://jihadwatch.org http://www.PetitionOnline.com/tap12009/ http://memri.org Rhinoceros has poor vision, but, due to his size, it's not his problem. __ R-help@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.
Re: [R] list to matrix?
Try: matrix(unlist(a), ncol=2, byrow=T) --Mark Lamias From: Sam Steingold s...@gnu.org To: r-help@r-project.org Sent: Tuesday, December 4, 2012 3:09 PM Subject: [R] list to matrix? How do I convert a list to a matrix? --8---cut here---start-8--- list(c(5, 101), c(1e+05, 46), c(15, 31), c(2e+05, 17), c(25, 19), c(3e+05, 11), c(35, 12), c(4e+05, 25), c(45, 19), c(5e+05, 16)) as.matrix(a) [,1] [1,] Numeric,2 [2,] Numeric,2 [3,] Numeric,2 [4,] Numeric,2 [5,] Numeric,2 [6,] Numeric,2 [7,] Numeric,2 [8,] Numeric,2 [9,] Numeric,2 --8---cut here---end---8--- thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://dhimmi.com http://jihadwatch.org http://www.PetitionOnline.com/tap12009/ http://memri.org Rhinoceros has poor vision, but, due to his size, it's not his problem. __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] list to matrix?
On Tue, Dec 4, 2012 at 8:09 PM, Sam Steingold s...@gnu.org wrote: How do I convert a list to a matrix? --8---cut here---start-8--- list(c(5, 101), c(1e+05, 46), c(15, 31), c(2e+05, 17), c(25, 19), c(3e+05, 11), c(35, 12), c(4e+05, 25), c(45, 19), c(5e+05, 16)) do.call(rbind, LIST) or do.call(cbind, LIST) depending on your desired direction. The do.call syntax takes a function name and uses a list as arguments to that function, returning the result. Super useful for these situations where you collect things and something like cbind(x[[1]],x[[2]], x[[3]]...) isn't feasible or possible. MW __ R-help@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.
Re: [R] list to matrix?
Hi, Try this: list1-list(c(5, 101), c(1e+05, 46), c(15, 31), c(2e+05, 17), c(25, 19), c(3e+05, 11), c(35, 12), c(4e+05, 25), c(45, 19), c(5e+05, 16)) res-t(sapply(list1,function(x) x)) res # [,1] [,2] #[1,] 5 101 #[2,] 10 46 #[3,] 15 31 #[4,] 20 17 #[5,] 25 19 #[6,] 30 11 #[7,] 35 12 #[8,] 40 25 #[9,] 45 19 #[10,] 50 16 is.matrix(res) #[1] TRUE #or res1-sapply(list1,function(x) x) is.matrix(res1) #[1] TRUE A.K. - Original Message - From: Sam Steingold s...@gnu.org To: r-help@r-project.org Cc: Sent: Tuesday, December 4, 2012 3:09 PM Subject: [R] list to matrix? How do I convert a list to a matrix? --8---cut here---start-8--- list(c(5, 101), c(1e+05, 46), c(15, 31), c(2e+05, 17), c(25, 19), c(3e+05, 11), c(35, 12), c(4e+05, 25), c(45, 19), c(5e+05, 16)) as.matrix(a) [,1] [1,] Numeric,2 [2,] Numeric,2 [3,] Numeric,2 [4,] Numeric,2 [5,] Numeric,2 [6,] Numeric,2 [7,] Numeric,2 [8,] Numeric,2 [9,] Numeric,2 --8---cut here---end---8--- thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://dhimmi.com http://jihadwatch.org http://www.PetitionOnline.com/tap12009/ http://memri.org Rhinoceros has poor vision, but, due to his size, it's not his problem. __ R-help@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. __ R-help@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.
Re: [R] list to matrix?
On Tue, Dec 4, 2012 at 8:17 PM, arun smartpink...@yahoo.com wrote: Hi, Try this: list1-list(c(5, 101), c(1e+05, 46), c(15, 31), c(2e+05, 17), c(25, 19), c(3e+05, 11), c(35, 12), c(4e+05, 25), c(45, 19), c(5e+05, 16)) res-t(sapply(list1,function(x) x)) Bah Humbug! (In Christmas cheer) No need for all this (see solutions including mine already given) -- but even without those, this is silly. An identity map is a real waste if you just want the simplification bit of sapply() -- you'd be much better just using simplify2array() Michael __ R-help@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.
Re: [R] list to matrix?
No need for all this (see solutions including mine already given) -- but even without those, this is silly. An identity map is a real waste if you just want the simplification bit of sapply() -- you'd be much better just using simplify2array() You are right that simplify2array(p) does everything that sapply(p,function(x)x) does and does it more quickly and that matrix(unlist(p),nrow=2,byrow=TRUE) is much faster than either: p - lapply(1:1e6, function(i)c(i, log2(i))) system.time(z1 - t(sapply(p,function(x)x))) user system elapsed 1.2 0.0 1.2 system.time(z2 - t(simplify2array(p))) user system elapsed 0.910.000.90 system.time(z3 - matrix(unlist(p), ncol=2, byrow=TRUE)) user system elapsed 0.040.000.04 You can also use vapply instead of sapply - it requires that you supply the expect shape and type of FUN's output so it is doesn't have to figure this out from looking at all the outputs of FUN: system.time(z4 - t(vapply(p,FUN=function(x)x,FUN.VALUE=numeric(2 user system elapsed 0.560.000.56 An advantage of vapply is that it stops in its tracks if FUN returns an unexpected value. sapply() and simplify2array() will silently give you an unexpected result (a single column matrix of mode list instead of a vector of numbers) and matrix(unlist()...) gives you a warning if you are lucky. pBad - p ; pBad[[length(pBad)/2]] - 666 system.time(zBad1 - t(sapply(pBad,function(x)x))) user system elapsed 1.750.001.75 zBad1[,49:51] # not what we wanted [[1]] [1] 49.0 18.93157 [[2]] [1] 666 [[3]] [1] 51.0 18.93157 system.time(zBad2 - t(simplify2array(pBad))) user system elapsed 0.5 0.0 0.5 system.time(zBad3 - matrix(unlist(pBad), ncol=2, byrow=TRUE)) user system elapsed 0.030.000.03 Warning message: In matrix(unlist(pBad), ncol = 2, byrow = TRUE) : data length [199] is not a sub-multiple or multiple of the number of rows [100] # no warning if length(unlist(p)) were even system.time(zBad4 - t(vapply(pBad,function(x)x,numeric(2 Error in vapply(pBad, function(x) x, numeric(2)) : values must be length 2, but FUN(X[[50]]) result is length 1 Timing stopped at: 0.29 0 0.28 Which of the latter two methods you choose depends on how likely errors in the data are. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of R. Michael Weylandt Sent: Tuesday, December 04, 2012 2:59 PM To: arun Cc: R help; s...@gnu.org Subject: Re: [R] list to matrix? On Tue, Dec 4, 2012 at 8:17 PM, arun smartpink...@yahoo.com wrote: Hi, Try this: list1-list(c(5, 101), c(1e+05, 46), c(15, 31), c(2e+05, 17), c(25, 19), c(3e+05, 11), c(35, 12), c(4e+05, 25), c(45, 19), c(5e+05, 16)) res-t(sapply(list1,function(x) x)) Bah Humbug! (In Christmas cheer) No need for all this (see solutions including mine already given) -- but even without those, this is silly. An identity map is a real waste if you just want the simplification bit of sapply() -- you'd be much better just using simplify2array() Michael __ R-help@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. __ R-help@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.
Re: [R] list to matrix?
Hi, p - lapply(1:1e6, function(i)c(i, log2(i))) system.time(z1 - t(sapply(p,function(x)x))) # user system elapsed # 2.568 0.048 2.619 system.time(z1 - do.call(rbind,p)) # user system elapsed # 4.000 0.052 4.060 A.K. - Original Message - From: William Dunlap wdun...@tibco.com To: R. Michael Weylandt michael.weyla...@gmail.com; arun smartpink...@yahoo.com Cc: R help r-help@r-project.org; s...@gnu.org s...@gnu.org Sent: Tuesday, December 4, 2012 6:28 PM Subject: RE: [R] list to matrix? No need for all this (see solutions including mine already given) -- but even without those, this is silly. An identity map is a real waste if you just want the simplification bit of sapply() -- you'd be much better just using simplify2array() You are right that simplify2array(p) does everything that sapply(p,function(x)x) does and does it more quickly and that matrix(unlist(p),nrow=2,byrow=TRUE) is much faster than either: p - lapply(1:1e6, function(i)c(i, log2(i))) system.time(z1 - t(sapply(p,function(x)x))) user system elapsed 1.2 0.0 1.2 system.time(z2 - t(simplify2array(p))) user system elapsed 0.91 0.00 0.90 system.time(z3 - matrix(unlist(p), ncol=2, byrow=TRUE)) user system elapsed 0.04 0.00 0.04 You can also use vapply instead of sapply - it requires that you supply the expect shape and type of FUN's output so it is doesn't have to figure this out from looking at all the outputs of FUN: system.time(z4 - t(vapply(p,FUN=function(x)x,FUN.VALUE=numeric(2 user system elapsed 0.56 0.00 0.56 An advantage of vapply is that it stops in its tracks if FUN returns an unexpected value. sapply() and simplify2array() will silently give you an unexpected result (a single column matrix of mode list instead of a vector of numbers) and matrix(unlist()...) gives you a warning if you are lucky. pBad - p ; pBad[[length(pBad)/2]] - 666 system.time(zBad1 - t(sapply(pBad,function(x)x))) user system elapsed 1.75 0.00 1.75 zBad1[,49:51] # not what we wanted [[1]] [1] 49.0 18.93157 [[2]] [1] 666 [[3]] [1] 51.0 18.93157 system.time(zBad2 - t(simplify2array(pBad))) user system elapsed 0.5 0.0 0.5 system.time(zBad3 - matrix(unlist(pBad), ncol=2, byrow=TRUE)) user system elapsed 0.03 0.00 0.03 Warning message: In matrix(unlist(pBad), ncol = 2, byrow = TRUE) : data length [199] is not a sub-multiple or multiple of the number of rows [100] # no warning if length(unlist(p)) were even system.time(zBad4 - t(vapply(pBad,function(x)x,numeric(2 Error in vapply(pBad, function(x) x, numeric(2)) : values must be length 2, but FUN(X[[50]]) result is length 1 Timing stopped at: 0.29 0 0.28 Which of the latter two methods you choose depends on how likely errors in the data are. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of R. Michael Weylandt Sent: Tuesday, December 04, 2012 2:59 PM To: arun Cc: R help; s...@gnu.org Subject: Re: [R] list to matrix? On Tue, Dec 4, 2012 at 8:17 PM, arun smartpink...@yahoo.com wrote: Hi, Try this: list1-list(c(5, 101), c(1e+05, 46), c(15, 31), c(2e+05, 17), c(25, 19), c(3e+05, 11), c(35, 12), c(4e+05, 25), c(45, 19), c(5e+05, 16)) res-t(sapply(list1,function(x) x)) Bah Humbug! (In Christmas cheer) No need for all this (see solutions including mine already given) -- but even without those, this is silly. An identity map is a real waste if you just want the simplification bit of sapply() -- you'd be much better just using simplify2array() Michael __ R-help@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. __ R-help@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.
[R] List or matrix of object
Hi everyone. Is it possible in R to create a matrix or a list (vector) or R object. For instance, I have f1 - function(x) sqrt(x%*%x); f2 - function(x) (2x+1); I would like to do something like L - List(); L[1] = f1; L[2] = f2; So, is there a way to create matrix or vector that can contains R object. With regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/List-or-matrix-of-object-tp2992101p2992101.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] List or matrix of object
You probably need to review the Intro to R to understand indexing: f1 - function(x) sqrt(x%*%x); f2 - function(x) (2*x+1); L - list() L[[1]] - f1 L[[2]] - f2 L # contains the objects [[1]] function (x) sqrt(x %*% x) [[2]] function (x) (2 * x + 1) L[[1]](3) # now call the functions in the list [,1] [1,]3 L[[2]](42) [1] 85 On Tue, Oct 12, 2010 at 11:17 AM, Filoche pmassico...@hotmail.com wrote: Hi everyone. Is it possible in R to create a matrix or a list (vector) or R object. For instance, I have f1 - function(x) sqrt(x%*%x); f2 - function(x) (2x+1); I would like to do something like L - List(); L[1] = f1; L[2] = f2; So, is there a way to create matrix or vector that can contains R object. With regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/List-or-matrix-of-object-tp2992101p2992101.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ R-help@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.
Re: [R] List or matrix of object
It is not clear exactly what you are trying to do, but this works: f1 - function(x) sqrt(x%*%x) f2 - function(x) {2*x+1} L - list(); L[[1]] = f1; L[[2]] = f2; Then you can do something like: L[[2]](5) -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of Filoche Sent: Tuesday, October 12, 2010 9:17 AM To: r-help@r-project.org Subject: [R] List or matrix of object Hi everyone. Is it possible in R to create a matrix or a list (vector) or R object. For instance, I have f1 - function(x) sqrt(x%*%x); f2 - function(x) (2x+1); I would like to do something like L - List(); L[1] = f1; L[2] = f2; So, is there a way to create matrix or vector that can contains R object. With regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/List-or- matrix-of-object-tp2992101p2992101.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
Re: [R] List or matrix of object
On Oct 12, 2010, at 11:17 AM, Filoche wrote: Hi everyone. Is it possible in R to create a matrix or a list (vector) or R object. For instance, I have f1 - function(x) sqrt(x%*%x); f2 - function(x) (2x+1); I would like to do something like L - List(); L[1] = f1; L[2] = f2; You should learn a few things (These should have been explained and illustrated as you worked your way through the An Introduction to R): http://cran.r-project.org/doc/manuals/R-intro.pdf ... R is case sensitive so list != List. Changing List to list would help. ... except for the fact that 2x is not a valid expression. Need 2*x ... and, the [- and [[- operations are different. The use of [[- works: f1 - function(x) sqrt(x%*%x) f2 - function(x) (2*x+1) L - list() L[[1]] - f1 L[[2]] - f2; L If you want to use [-, you will need to give it a valid list object: f1 - function(x) sqrt(x%*%x) f2 - function(x) (2*x+1) L - list() L[1] - list(f1) L[2] - list(f2) L ... and drop the use ;'s at the end of lines. -- So, is there a way to create matrix or vector that can contains R object. With regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/List-or-matrix-of-object-tp2992101p2992101.html Sent from the R help mailing list archive at Nabble.com. . David Winsemius, MD West Hartford, CT __ R-help@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.
Re: [R] List or matrix of object
Hi again everyone. I found I could use a list with l = list() l[[1]] = myObj instead of l[1] = myObj Anyone can explain me why the use of double [] is required? Regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/List-or-matrix-of-object-tp2992101p2992121.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] List or matrix of object
The difference is the same as the difference between a set with 1 element and a single element from a set. The single [ extracts/replaces/assigns a subset of the list elements, but the piece is still a list (even if it is one element). So when you are assigning using [ you need to give it a list, not a single object. If you create a list then do something like mode( mylist[1] ) you will see that it is still a list. The double [[ exctracts/replaces/assigns a single element of the list, it does not work on anything more than a single element, but it works with that element as its own object, not a list (unless it is a list). So [[ can be used to assign a single element without needing to create a list (but can only do a single element where [ can do 1 or more). If you do mode( mylist[[1]] ) then you will see that the single element is no longer a list. Hope that helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of Filoche Sent: Tuesday, October 12, 2010 9:29 AM To: r-help@r-project.org Subject: Re: [R] List or matrix of object Hi again everyone. I found I could use a list with l = list() l[[1]] = myObj instead of l[1] = myObj Anyone can explain me why the use of double [] is required? Regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/List-or- matrix-of-object-tp2992101p2992121.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
Re: [R] List or matrix of object
Thank you everyone for your answers. Regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/List-or-matrix-of-object-tp2992101p2992304.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] List to matrix or to vectors conversion
Dear list, I have a list with 1000 x1000 lines and columns do you know how I can convert it to matrrix or data.frame. Thanks. Juan -- Juan Tomás Sayago Universidad Central http://sites.google.com/site/juantomassayago/ Objetivo: Garantizar a cada ser humano que habite en el país, una cantidad mínima de agua con calidad segura para el consumo humano, en forma regular, permanente y suficiente para la vida y la salud [[alternative HTML version deleted]] __ R-help@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.
Re: [R] List to matrix or to vectors conversion
On 12-Feb-10 13:14:29, Juan Tomas Sayago wrote: Dear list, I have a list with 1000 x1000 lines and columns do you know how I can convert it to matrrix or data.frame. Thanks. Juan as.data.frame() will convert it to a dataframe. If you then apply as.matrix() to the result you will get a matrix: L - list(X=c(1,2,3),Y=c(4,5,6),Z=c(7,8,9)) L # $X # [1] 1 2 3 # $Y # [1] 4 5 6 # $Z # [1] 7 8 9 D - as.data.frame(L) D # X Y Z # 1 1 4 7 # 2 2 5 8 # 3 3 6 9 M - as.matrix(D) M # X Y Z # [1,] 1 4 7 # [2,] 2 5 8 # [3,] 3 6 9 Note that applying as.matrix() directly to the original L will not work. It returns a list, not a matrix. Ted. E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 12-Feb-10 Time: 13:40:32 -- XFMail -- __ R-help@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.
Re: [R] List to matrix or to vectors conversion
Ted.Harding at manchester.ac.uk writes: On 12-Feb-10 13:14:29, Juan Tomas Sayago wrote: Dear list, I have a list with 1000 x1000 lines and columns Lists have neither lines nor columns. Can you explain exactly what you have? E.g. show us the code that created your list? do you know how I can convert it to matrrix or data.frame. Thanks. Juan as.data.frame() will convert it to a dataframe. If you then apply as.matrix() to the result you will get a matrix: L - list(X=c(1,2,3),Y=c(4,5,6),Z=c(7,8,9)) If you want a matrix as opposed to a data.frame (e.g. your list entries are all numeric), and the data set is large, this more efficient method might be useful: matrix(unlist(L), nrow=3) [,1] [,2] [,3] [1,]147 [2,]258 [3,]369 If it's not obvious to you what that does, consider: unlist(L) X1 X2 X3 Y1 Y2 Y3 Z1 Z2 Z3 1 2 3 4 5 6 7 8 9 matrix(unlist(L), nrow=3, byrow=TRUE) [,1] [,2] [,3] [1,]123 [2,]456 [3,]789 matrix(unlist(L), nrow=3, byrow=FALSE) [,1] [,2] [,3] [1,]147 [2,]258 [3,]369 L # $X # [1] 1 2 3 # $Y # [1] 4 5 6 # $Z # [1] 7 8 9 D - as.data.frame(L) D # X Y Z # 1 1 4 7 # 2 2 5 8 # 3 3 6 9 M - as.matrix(D) M # X Y Z # [1,] 1 4 7 # [2,] 2 5 8 # [3,] 3 6 9 Note that applying as.matrix() directly to the original L will not work. It returns a list, not a matrix. Ted. E-Mail: (Ted Harding) Ted.Harding at manchester.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 12-Feb-10 Time: 13:40:32 -- XFMail -- __ R-help@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.
Re: [R] list to Matrix - remove NAs von list.
Another alternative: do.call('cbind', l[!sapply(l, function(x)all(is.na(x)))]) On 11/2/07, Petr PIKAL [EMAIL PROTECTED] wrote: Hi [EMAIL PROTECTED] napsal dne 02.11.2007 12:00:09: Thanks, I have the case that there is a NA in the list. This should not be a column. But na.omit(l) does not work for lists. How to remove NAs from a list? l - list(c(1,2,3),NA,c(1,2,3)) mat - do.call(cbind, l) If number of NA is not big you can use mat[,which(!is.na(colSums(mat)))] to select non NA columns. Regards Petr Best Markus Dimitris Rizopoulos schrieb: you can use do.call(), e.g., do.call(cbind, l) 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 Quoting Markus Schmidberger [EMAIL PROTECTED]: Hello, I have a list of vectors (all the same length). How to convert the list to a matrix? Each vector should be a column. I tried this: l - list(c(1,2,3),c(1,2,3),c(1,2,3)) mat - matrix( unlist(l), nrow=length(l) ) But I think this is not very efficient. Is there a better solution? Thanks Markus -- Dipl.-Tech. Math. Markus Schmidberger Ludwig-Maximilians-Universität München IBE - Institut für medizinische Informationsverarbeitung, Biometrie und Epidemiologie Marchioninistr. 15, D-81377 Muenchen URL: http://ibe.web.med.uni-muenchen.de Mail: Markus.Schmidberger [at] ibe.med.uni-muenchen.de __ R-help@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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm -- Dipl.-Tech. Math. Markus Schmidberger Ludwig-Maximilians-Universität München IBE - Institut für medizinische Informationsverarbeitung, Biometrie und Epidemiologie Marchioninistr. 15, D-81377 Muenchen URL: http://ibe.web.med.uni-muenchen.de Mail: Markus.Schmidberger [at] ibe.med.uni-muenchen.de Tel: +49 (089) 7095 - 4599 __ R-help@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. __ R-help@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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@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.
Re: [R] list to Matrix
you can use do.call(), e.g., do.call(cbind, l) 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 Quoting Markus Schmidberger [EMAIL PROTECTED]: Hello, I have a list of vectors (all the same length). How to convert the list to a matrix? Each vector should be a column. I tried this: l - list(c(1,2,3),c(1,2,3),c(1,2,3)) mat - matrix( unlist(l), nrow=length(l) ) But I think this is not very efficient. Is there a better solution? Thanks Markus -- Dipl.-Tech. Math. Markus Schmidberger Ludwig-Maximilians-Universität München IBE - Institut für medizinische Informationsverarbeitung, Biometrie und Epidemiologie Marchioninistr. 15, D-81377 Muenchen URL: http://ibe.web.med.uni-muenchen.de Mail: Markus.Schmidberger [at] ibe.med.uni-muenchen.de __ R-help@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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@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.
[R] list to Matrix
Hello, I have a list of vectors (all the same length). How to convert the list to a matrix? Each vector should be a column. I tried this: l - list(c(1,2,3),c(1,2,3),c(1,2,3)) mat - matrix( unlist(l), nrow=length(l) ) But I think this is not very efficient. Is there a better solution? Thanks Markus -- Dipl.-Tech. Math. Markus Schmidberger Ludwig-Maximilians-Universität München IBE - Institut für medizinische Informationsverarbeitung, Biometrie und Epidemiologie Marchioninistr. 15, D-81377 Muenchen URL: http://ibe.web.med.uni-muenchen.de Mail: Markus.Schmidberger [at] ibe.med.uni-muenchen.de __ R-help@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.