Hi, I can't seem to figure out how to tell R to stop expanding an object. I would like to use the literal name rather than the expanded value. The issue occurs in a function I've been writing. The problematic part looks like this: # "fund" is a matrix of open, high, low, close, and volume prices returns <- function(fund) { p_12ago = as.vector(fund[nrow(fund)-252,6]) perc_diff_12mo = ((p_last - p_12ago) / p_last)*100 #This is the line that's giving me problems: funds=c(as.character(fund)) mo12=c(perc_diff_12mo) final_results=data.frame(funds,mo12) return(final_results) } I can't figure out how to insert the original argument from the function (in this case the name of the fund). Ultimately I want to be able to do returns("GOOG"), and have the output in this format: funds 12mo GOOG 14% I've tried using as.character(), quote(), dQuote(), sQuote(), gsub(), cat(), and a bunch of combinations of them. I'm brand new to R, so I apologize if I'm going about this in the wrong way. Thanks in advance for any help! Kevin -- View this message in context: http://old.nabble.com/How-to-interpret-the-name-of-an-object-literally--tp26499906p26499906.html Sent from the R help mailing list archive at Nabble.com.
Duncan Murdoch
2009-Nov-24 20:07 UTC
[R] How to interpret the name of an object literally?
On 11/24/2009 1:13 PM, novocaine wrote:> Hi, > > I can't seem to figure out how to tell R to stop expanding an object. I > would like to use the literal name rather than the expanded value. > > The issue occurs in a function I've been writing. The problematic part looks > like this: > > # "fund" is a matrix of open, high, low, close, and volume prices > returns <- function(fund) > { > p_12ago = as.vector(fund[nrow(fund)-252,6]) > perc_diff_12mo = ((p_last - p_12ago) / p_last)*100 > > #This is the line that's giving me problems: > funds=c(as.character(fund)) > > mo12=c(perc_diff_12mo) > final_results=data.frame(funds,mo12) > return(final_results) > } > > I can't figure out how to insert the original argument from the function (in > this case the name of the fund). > > Ultimately I want to be able to do returns("GOOG"), and have the output in > this format: > > funds 12mo > GOOG 14% > > I've tried using as.character(), quote(), dQuote(), sQuote(), gsub(), cat(), > and a bunch of combinations of them. I'm brand new to R, so I apologize if > I'm going about this in the wrong way. > > Thanks in advance for any help!Use deparse(substitute(fund)). The inner function gives you the expression passed as fund, the deparse() turns it into a string. You can in weird cases get more than one line of output from deparse; you could use the width.cutoff and/or nlines arguments to control this. Duncan Murdoch
Apparently Analagous Threads
- Symlinks followed literally in Debian 3.0, not in Red Hat 7.3
- Symlinks followed literally in Debian 3.0, not in Red Hat 7.3
- printCoefmat() for a data.frame with factors
- How to extract function arguments literally
- plyr: a*ply with functions that return matrices-- possible bug in aaply?