[R] Variable names AS variable names?
David Winsemius
dwinsemius at comcast.net
Fri Feb 25 19:09:36 CET 2011
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). )
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
More information about the R-help
mailing list