# [R] eval and as.name

Fuchs Ira irafuchs at gmail.com
Fri Feb 6 01:49:20 CET 2009

```VERY nice.  That certainly works. Now how does one plot all three
variables on one graph? :-)

Thanks very much.

Ira

On Feb 5, 2009, at 7:29 PM, Phil Spector wrote:

> Ira -
>   As you say, there are many ways to solve this problem.
> Here's one of them:
>
> list=c("MSFT","AAPL","ORCL")
> getSymbols(list)
>
> #First, create a function that will take the name of a variable,
> #get the Close value from it and pass it to the Delt function:
>
> makedelt = function(l){now = get(l) ;
> Delt(now[,paste(l,".Close",sep='')])}
>
> #Now create a list of the results from Delt:
>
> allclose = lapply(list,makedelt)
>
> #Finally pass the list to Reduce to merge them all together:
>
> result = Reduce(merge,allclose)
>
> #To better identify the columns, you can use
>
> colnames(result) = list
>
> Hope this helps.
>                                       - Phil Spector
> 					 Statistical Computing Facility
> 					 Department of Statistics
> 					 UC Berkeley
> 					 spector at stat.berkeley.edu
>
>
>
> On Thu, 5 Feb 2009, Fuchs Ira wrote:
>
>> These are all great resposes but I think I should have given a
>> slightly more complete description of the problem.
>>
>> I am using the quantmod package and I have called getSymbols with a
>> character vector that has a list of stock symbols. For example:
>>
>> library("quantmod")
>> list=c("MSFT","AAPL","ORCL")
>> getSymbols(list)
>>
>> this returns 3 objects (MSFT,AAPL, ORCL)
>>
>> now imagine that I want to use the Delt function (also in quantmod)
>> to
>> calculate the percent difference for each of these stocks using their
>> closing price.
>>
>> each closing price is SYMBOL\$SYMBOL.Close (can also be retrieved with
>> Cl(SYMBOL)
>>
>> I want to construct the call to Delt with the symbols in list
>> appended
>>
>> in other words I want a way to create
>>
>> Delt(MSFT\$MSFT.Close) merged with Delt(AAPL\$AAPL.Close) and so on
>> (one object having the result of the Delt function for each stock).
>>
>> I am sure that there are many ways to solve this particular problem.
>> Perhaps what I need to understand is how to take the characters in
>> the
>> list and construct an expression  and then evaluate the expression.
>>
>>
>> On Feb 5, 2009, at 5:50 PM, <markleeds at verizon.net> wrote:
>>
>>> Hi:  there's mget but I couldn't figure out how to use it. if you
>>> figure
>>> it out, let me know.
>>> I'm sure one of the guRus will reply with something that uses mget.
>>> List
>>> is slow now
>>> because Europe is sleeping. Good luck.
>>>
>>>
>>>
>>>
>>> On Thu, Feb 5, 2009 at  5:42 PM, Ira Fuchs wrote:
>>>
>>>> Thanks, that is better but doesn't actually solve my problem which
>>> is
>>>> that n is an arbitrary length vector and I'd like to find a way
>>>> that
>>>> avoids having to enumerate the elements of the character vector.
>>>> Thanks,
>>>> Ira
>>>> ----- Original Message -----
>>>> From: markleeds at verizon.net <markleeds at verizon.net>
>>>> To: Fuchs Ira <irafuchs at gmail.com>
>>>> Sent: Thu Feb 05 17:25:39 2009
>>>> Subject: RE: [R] eval and as.name
>>>>
>>>>
>>>> Hi: below works but it's not much shorter than yours.  there must
>>> be a
>>>> better way so I'm sending off line in order to encourage better
>>>> replies.
>>>>
>>>> sum(get(n[1]),get(n[2]))
>>>>
>>>>
>>>>
>>>>
>>>> On Thu, Feb 5, 2009 at  5:10 PM, Fuchs Ira wrote:
>>>>
>>>>> I'm sure there is a more general way to ask this question but how
>>> do
>>>>> you use the elements of a character vector as names of objects in
>>> an
>>>>> expression?
>>>>> For example, say you have:
>>>>>
>>>>> a = c(1,3,5,7)
>>>>> b = c(2,4,6,8)
>>>>>
>>>>> n=c("a","b")
>>>>>
>>>>> and you want to use the names a and b in a function (e.g. sum)
>>>>>
>>>>> sum(eval(as.name(n[1])),eval(as.name(n[2])))
>>>>>
>>>>> works but
>>>>>
>>>>> what is a simpler way to effect this level of indirection?
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help