erwann rogard
2008-Oct-20 06:06 UTC
[R] Extracting variables with a given prefix in the parent environment
hi, get.vars.name.prefix<-function(prefix){ result<-list() len<-nchar(prefix) var.names<-ls(name=1,pattern=prefix) #name=1 probably wrong option print(var.names) for(i in 1:length(var.names)){ name<-var.names[i] field<-substr(name,len+1,nchar(name)) result[[field]]<-get(name) } result } for example x.1<-1 x.2<-2 get.vars.name.prefix("x.") should return 1,2 this does not work if i call if from within another function: g<-function(){ x.1<-1 x.2<-2 get.vars.name.prefix("x.") # should return 1,2, nothing else } i would like the environment specified within ls(...) to be that of the body of the function that calls get.vars.name.prefix. apparently name=1 is probably not the right option. thanks. [[alternative HTML version deleted]]
Duncan Murdoch
2008-Oct-20 10:31 UTC
[R] Extracting variables with a given prefix in the parent environment
On 20/10/2008 2:06 AM, erwann rogard wrote:> hi, > > get.vars.name.prefix<-function(prefix){ > result<-list() > len<-nchar(prefix) > var.names<-ls(name=1,pattern=prefix) #name=1 probably wrong option > print(var.names) > for(i in 1:length(var.names)){ > name<-var.names[i] > field<-substr(name,len+1,nchar(name)) > result[[field]]<-get(name) > } > result > } > > for example > x.1<-1 > x.2<-2 > get.vars.name.prefix("x.") should return 1,2 > > this does not work if i call if from within another function: > > g<-function(){ > x.1<-1 > x.2<-2 > get.vars.name.prefix("x.") # should return 1,2, nothing else > } > > i would like the environment specified within ls(...) to be that of the body > of the function that calls get.vars.name.prefix. apparently name=1 is > probably not the right option.Use ls(name=parent.frame(), pattern=...) And be careful about the pattern: it's a regular expression, so you would need to start with a caret "^" to guarantee that it occurs at the start of the name. Duncan Murdoch
Henrique Dallazuanna
2008-Oct-20 10:32 UTC
[R] Extracting variables with a given prefix in the parent environment
Try this: Using 'envir' argument, in ls and get function: get.vars.name.prefix<-function(prefix, envir = parent.frame()){ result<-list() len<-nchar(prefix) var.names<-ls(envir = envir,pattern=prefix) print(var.names) for(i in 1:length(var.names)){ name<-var.names[i] field<-substr(name,len+1,nchar(name)) result[[field]]<-get(name, envir = envir) } result } On Mon, Oct 20, 2008 at 4:06 AM, erwann rogard <erwann.rogard@gmail.com>wrote:> hi, > > get.vars.name.prefix<-function(prefix){ > result<-list() > len<-nchar(prefix) > var.names<-ls(name=1,pattern=prefix) #name=1 probably wrong option > print(var.names) > for(i in 1:length(var.names)){ > name<-var.names[i] > field<-substr(name,len+1,nchar(name)) > result[[field]]<-get(name) > } > result > } > > for example > x.1<-1 > x.2<-2 > get.vars.name.prefix("x.") should return 1,2 > > this does not work if i call if from within another function: > > g<-function(){ > x.1<-1 > x.2<-2 > get.vars.name.prefix("x.") # should return 1,2, nothing else > } > > i would like the environment specified within ls(...) to be that of the > body > of the function that calls get.vars.name.prefix. apparently name=1 is > probably not the right option. > > thanks. > > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]