Re: [R] Automate concatenation?
Paul == Paul Roebuck [EMAIL PROTECTED] on Tue, 30 May 2006 16:04:14 -0500 (CDT) writes: Paul On Tue, 30 May 2006, Robert Lundqvist wrote: I have this typical problem of joining a number of vectors with similar names - a1, a2,..., a10 - which should be concatenated into one. Using c(a1,a2,a3,a4,a5,a6,a,a8,a9,a10) naturally works, but I would like to do it with less manual input. My attempts to use paste() gives a vector of the vector names, see below. The question is how to do the the concatenation? Any suggestions? paste(a,1:10,sep=) Paul a1 - c(5, 4) Paul a2 - 2 Paul a3 - 6:9 Paul cmd - sprintf(c(%s), Paulpaste(a, 1:3, sep = , collapse = , )) Paul eval(parse(text = cmd)) Executive Summary: Try not to use eval(parse(text = * )) Excuse me, Paul, but the other replies to this message had better answers. If you do install.packages(fortunes) library(fortunes) fortune(answer is parse) You get a very notable advise (by Thomas Lumley) on why a solution using parse() very often ``stinks''. In the present example and quite a few other ones, the much better solution is to use get() [as the other replies in this thread do]. In a recent private e-mail, Brian Ripley has reminded me of the generic way to avoid eval(parse(text = * )) , namely via eval(substitute(f(A,B), list(A=.., B=..)) Martin Maechler, ETH Zurich __ R-help@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
[R] Automate concatenation?
I have this typical problem of joining a number of vectors with similar names - a1, a2,..., a10 - which should be concatenated into one. Using c(a1,a2,a3,a4,a5,a6,a,a8,a9,a10) naturally works, but I would like to do it with less manual input. My attempts to use paste() gives a vector of the vector names, see below. The question is how to do the the concatenation? Any suggestions? paste(a,1:10,sep=) Robert __ R-help@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
Re: [R] Automate concatenation?
you need get(), look also at FAQ 7.21 a1 - 1:3 a2 - 4:5 a3 - 6:10 a4 - 11:20 a5 - 21:25 # lapply(paste(a, 1:5, sep = ), get) 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: Robert Lundqvist [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Tuesday, May 30, 2006 2:07 PM Subject: [R] Automate concatenation? I have this typical problem of joining a number of vectors with similar names - a1, a2,..., a10 - which should be concatenated into one. Using c(a1,a2,a3,a4,a5,a6,a,a8,a9,a10) naturally works, but I would like to do it with less manual input. My attempts to use paste() gives a vector of the vector names, see below. The question is how to do the the concatenation? Any suggestions? paste(a,1:10,sep=) Robert __ R-help@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 Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@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
Re: [R] Automate concatenation?
Robert Lundqvist [EMAIL PROTECTED] writes: I have this typical problem of joining a number of vectors with similar names - a1, a2,..., a10 - which should be concatenated into one. Using c(a1,a2,a3,a4,a5,a6,a,a8,a9,a10) naturally works, but I would like to do it with less manual input. And less error-prone (where did the 7 go?)... My attempts to use paste() gives a vector of the vector names, see below. The question is how to do the the concatenation? Any suggestions? paste(a,1:10,sep=) I think this should work: unlist(lapply( paste(a,1:10,sep=), get)) (and of course, the usual sermon applies: You're likely better off using a list of vectors rather than vectors with similar names.) -- O__ Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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
Re: [R] Automate concatenation?
On Tue, 30 May 2006, Robert Lundqvist wrote: I have this typical problem of joining a number of vectors with similar names - a1, a2,..., a10 - which should be concatenated into one. Using c(a1,a2,a3,a4,a5,a6,a,a8,a9,a10) naturally works, but I would like to do it with less manual input. My attempts to use paste() gives a vector of the vector names, see below. The question is how to do the the concatenation? Any suggestions? paste(a,1:10,sep=) a1 - c(5, 4) a2 - 2 a3 - 6:9 cmd - sprintf(c(%s), paste(a, 1:3, sep = , collapse = , )) eval(parse(text = cmd)) -- SIGSIG -- signature too long (core dumped) __ R-help@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
Re: [R] Automate concatenation?
Well, I like do.call(): puddy.tat - do.call(c,lapply(paste(a,1:10,sep=),get)) cheers, Rolf Turner [EMAIL PROTECTED] __ R-help@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