Hello Noah,

Many of these suggestions are already good, (and possibly better than you
think, depending on your level of experience).

Using lists helps a lot, because the elements of the list have easily
accessible names, and you can query the nodes to determine how many
variables you have in each category of the list.

It's also useful to consider the "deparse(substitute(x))" trick.  This lets
you grab the variable name for x within a function.

I also wanted to show you an example, maybe this will help.
This example doesn't use either of my suggestions.  Those are complicated
enough that they're hard to show in a short example.
This example does show how to use my third tip, which is using the eval
function



library(splines)

## Make up some data
set.seed(10)
dat = data.frame(
    y=runif(100),
    v1=rnorm(100,10,4),
    v2=rpois(100, 8),
    v3=sample(letters[1:4], replace=T))
dat

## Make formula parts
xNames = colnames(dat)[-1]
part1='model = lm(y ~ '
part2=list()
degrees = c(3,4,2)
for(i in 1:length(xNames))
    part2[[i]]=paste('bs(', xNames[i],
            ', degree=', degrees[i], ')', sep='')
part2 = paste(part2, collapse=' + ')
part3 = ', data=dat)'

## Merge parts
ModelFormula = paste(part1, part2, part3, sep='')
ModelFormula

## Whoops... can't do spline on v3, so fix v3
ModelFormula = gsub('bs\\(v3','v3',ModelFormula)
ModelFormula = gsub('v3, degree=[0-9]\\)','v3',ModelFormula)
ModelFormula

## Run Model
ModelResults = eval(parse(text=ModelFormula))

## View Results
summary(ModelResults)



On Fri, Feb 25, 2011 at 1:49 PM, David Winsemius <dwinsemius@comcast.net>wrote:

>
> On Feb 25, 2011, at 1:09 PM, David Winsemius wrote:
>
>
>> On Feb 25, 2011, at 12:33 PM, Noah Silverman wrote:
>>
>>  My actual code is several things with adaptive filtering.  This will
>>> require accessing data sporadically.  The loop was just a quick example
>>> for the e-mail.
>>>
>>> One application is to work with online (streaming) data.  If I get a new
>>> data point in for code "a1", I'll need to be able to reference the
>>> matrix named "a1".
>>>
>>
>> So, how do these things you are calling "codes" get their names?
>>
>> ("code" is not an R datatype. a1 is a matrix,  "a1" is a character value
>> and it would be returned by names(a1).  )
>>
>
> That's not correct. names(a1) would not return "a1" but names(codes)[1]
> would if defined as a list as below.
>
>
>
>> a1 <- matrix(rnorm(100), nrow=10)
>> b24 <- matrix(rnorm(100), nrow=10)
>> q99 <- matrix(rnorm(100), nrow=10)
>>
>> codes <- list(a1=a1, b24=b24, q99=q99)
>>
>> str(codes[['a1']])
>> ... should be a matrix
>>
>> Assignment also works with "[[" or with "[".
>>
>> We really _do_ need  examples that represent the problems posed on the
>> list. You have been posting a sufficient number of times to have understood
>> this by now.
>>
>> -- David
>>
>>
>>
>>> On 2/25/11 12:23 AM, David Winsemius wrote:
>>>
>>>>
>>>> On Feb 25, 2011, at 1:55 AM, Noah Silverman wrote:
>>>>
>>>>  How can I dynamically use a variable as the name for another variable?
>>>>>
>>>>> I realize this sounds cryptic, so an example is best:
>>>>>
>>>>> #Start with an array of "codes"
>>>>>
>>>>>
>>>> Is there some reason not to use list(a1, b24, q99)? If not then:
>>>>
>>>> lapply(codes, somefun)
>>>>
>>>>
>>>>
>>>>> #Each code has a corresponding matrix (could be vector)
>>>>> a1 <- matrix(rnorm(100), nrow=10)
>>>>> b24 <- matrix(rnorm(100), nrow=10)
>>>>> q99 <- matrix(rnorm(100), nrow=10)
>>>>>
>>>>> #Now, I want to loop through all the codes and do something with each
>>>>> matrix
>>>>> for(code in codes){
>>>>>  #here is where I'm stuck.  I don't want the value of code, but the
>>>>> variable who's name is the value of code
>>>>>
>>>>> }
>>>>>
>>>>>
>>>>> Any suggestions?
>>>>>
>>>>> -N
>>>>>
>>>>>
>> 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.
>>
>
> 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.
>

	[[alternative HTML version deleted]]

