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
nces@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 m
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
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
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
5 matches
Mail list logo