[R] assign value to multiple objects with a given ls pattern
Dear R colleagues, This seems pretty straight forward but I have been banging my head on this for some time and can't seem to find a solution suppose I have something like a1-1; a2-2; a3-3; a4-4; b1-3; b2-4 I would like to quickly assign to objects with a certain pattern, e.g., those in ls(pattern=a) a specific value, e.g., 99, without having to assign each object at a time. is there a way I can do this within a for cycle? I have tested several eval and parse statements but with no success. Regards, Nuno Prista PhD student Instituto de Oceanografia Faculdade de Ciências da Universidade de Lisboa Campo Grande, Lisboa, 1749-016 Lisboa, Portugal __ 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] assign value to multiple objects with a given ls pattern
Hi Nuno, Yes, you can do for(i in ls(pattern=a)) { assign(i, 99) } but honestly this is a bad idea. It will try to assign the value of 99 to any object in your workspace that contains an a, which sounds really scary to me. Better I think to use a list: ab.list - list(a1=1, a2=2, a3=3, a4=4, b1=3, b2=4) ab.list[grep(a, names(ab.list))] - 99 Or even just a named vector ab.vector - c(a1=1, a2=2, a3=3, a4=4, b1=3, b2=4) ab.vector[grep(a, names(ab.vector))] - 99 Best, Ista On Mon, Feb 21, 2011 at 4:22 PM, Nuno Prista nmpri...@fc.ul.pt wrote: Dear R colleagues, This seems pretty straight forward but I have been banging my head on this for some time and can't seem to find a solution suppose I have something like a1-1; a2-2; a3-3; a4-4; b1-3; b2-4 I would like to quickly assign to objects with a certain pattern, e.g., those in ls(pattern=a) a specific value, e.g., 99, without having to assign each object at a time. is there a way I can do this within a for cycle? I have tested several eval and parse statements but with no success. Regards, Nuno Prista PhD student Instituto de Oceanografia Faculdade de Ciências da Universidade de Lisboa Campo Grande, Lisboa, 1749-016 Lisboa, Portugal __ 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. -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org __ 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] assign value to multiple objects with a given ls pattern
Hi, This works for me: pat - ls(pattern=^a) ## I would anchor a to the beginning with ^ for safety! for (i in seq_along(pat))assign(pat[i], value=99) Or this with lapply: lapply(pat, FUN=function(x) assign(x, value=99, envir=.GlobalEnv)) See ?assign HTH, Ivan Le 2/21/2011 17:22, Nuno Prista a écrit : Dear R colleagues, This seems pretty straight forward but I have been banging my head on this for some time and can't seem to find a solution suppose I have something like a1-1; a2-2; a3-3; a4-4; b1-3; b2-4 I would like to quickly assign to objects with a certain pattern, e.g., those in ls(pattern=a) a specific value, e.g., 99, without having to assign each object at a time. is there a way I can do this within a for cycle? I have tested several eval and parse statements but with no success. Regards, Nuno Prista PhD student Instituto de Oceanografia Faculdade de Ciências da Universidade de Lisboa Campo Grande, Lisboa, 1749-016 Lisboa, Portugal __ 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. -- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calan...@uni-hamburg.de ** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php __ 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] assign value to multiple objects with a given ls pattern
If instead of having a1, a2, etc. as global variables you put them into a list then this becomes simple. The general rule is that if you ever want to do the same (or similar) think to a set of variable, then they should not have been separate variables, but part of a bigger one. Lists work well for this (there are other possibilities as well, but lists cover most of the cases). -- 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-bounces@r- project.org] On Behalf Of Nuno Prista Sent: Monday, February 21, 2011 9:22 AM To: r-help@R-project.org Subject: [R] assign value to multiple objects with a given ls pattern Dear R colleagues, This seems pretty straight forward but I have been banging my head on this for some time and can't seem to find a solution suppose I have something like a1-1; a2-2; a3-3; a4-4; b1-3; b2-4 I would like to quickly assign to objects with a certain pattern, e.g., those in ls(pattern=a) a specific value, e.g., 99, without having to assign each object at a time. is there a way I can do this within a for cycle? I have tested several eval and parse statements but with no success. Regards, Nuno Prista PhD student Instituto de Oceanografia Faculdade de Ciências da Universidade de Lisboa Campo Grande, Lisboa, 1749-016 Lisboa, Portugal __ 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] assign value to multiple objects with a given ls pattern
As I partially showed, I guess that the problem of assigning the value 99 to any object in the workspace can be dealt with a more precise regular expression. Maybe that would do: ls(pattern=^a[1-9]$) or ls(pattern=^a[0-9]+$) In any case, I agree that alternatives are better Ivan Le 2/21/2011 17:55, Ista Zahn a écrit : Hi Nuno, Yes, you can do for(i in ls(pattern=a)) { assign(i, 99) } but honestly this is a bad idea. It will try to assign the value of 99 to any object in your workspace that contains an a, which sounds really scary to me. Better I think to use a list: ab.list- list(a1=1, a2=2, a3=3, a4=4, b1=3, b2=4) ab.list[grep(a, names(ab.list))]- 99 Or even just a named vector ab.vector- c(a1=1, a2=2, a3=3, a4=4, b1=3, b2=4) ab.vector[grep(a, names(ab.vector))]- 99 Best, Ista On Mon, Feb 21, 2011 at 4:22 PM, Nuno Pristanmpri...@fc.ul.pt wrote: Dear R colleagues, This seems pretty straight forward but I have been banging my head on this for some time and can't seem to find a solution suppose I have something like a1-1; a2-2; a3-3; a4-4; b1-3; b2-4 I would like to quickly assign to objects with a certain pattern, e.g., those in ls(pattern=a) a specific value, e.g., 99, without having to assign each object at a time. is there a way I can do this within a for cycle? I have tested several eval and parse statements but with no success. Regards, Nuno Prista PhD student Instituto de Oceanografia Faculdade de Ciências da Universidade de Lisboa Campo Grande, Lisboa, 1749-016 Lisboa, Portugal __ 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. -- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calan...@uni-hamburg.de ** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php __ 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.