Dear ALL, I need a function that takes character string as argument value and replaces corresponding argument( which is behind dollar sign "$") in function body with its value. For example: function(dataframe, argument="ANYTHING I WANT") { return(dataframe$argument) } Even though I know there must be lots of possible alternative ways to do this, I really want to do the job just like the form in the above example and which I believe that has a close connection with "Programming on language" style(I read the term on some guides but really got no clue whether I refer to it here in a right way). Anyone who happens to know the method or the mechanism please kindly let me know. Thank you very much, and please reply in very simple English, because i am a poor English user. Thanks again, Xie YM
David Winsemius
2013-Feb-06 06:22 UTC
[R] How to dynamically replace the text behind dollar sign: '$'
On Feb 5, 2013, at 8:07 PM, ??? wrote:> Dear ALL, > > I need a function that takes character string as argument value and > replaces corresponding argument( which is behind dollar sign "$") in > function body with its value. > > For example: > > function(dataframe, argument="ANYTHING I WANT") > { > return(dataframe$argument) > } > > Even though I know there must be lots of possible alternative ways > to do this, I really want to do the job just like the form in the > above example and which I believe that has a close connection with > "Programming on language" style(I read the term on some guides but > really got no clue whether I refer to it here in a right way). > Anyone who happens to know the method or the mechanism please kindly > let me know. Thank you very much, and please reply in very simple > English, because i am a poor English user.The "$" construction is only a syntactic shortcut for "[["argument"]]. What you are looking for is described in the ?"[[" page. You should be constructing a formal that is assigned a syntactically legal column name ... NOT "ANYTHING I WANT". Perhaps: function(dfrm , colname = "V1") { return( dfrm[[ colname ]] ) # the argument will get evaluated as "V1" unless an alternate colname argument is supplied } ( You can't get "$" to evaluate a character column name.) -- David Winsemius, MD Alameda, CA, USA
Apparently Analagous Threads
- Useradd doesn't accept dollar sign ($) and "add machine script" doesn't work
- [klibc:update-dash] [PARSER] Handle backslash newlines properly after dollar sign
- [klibc:update-dash] dash: [PARSER] Handle backslash newlines properly after dollar sign
- file-attr-restore and dollar signs
- need help formating plot axis to standard dollar formats