I think you're looking for ?bquote .

Something like this should give you the idea:

nm <- "yvar"
bquote( .(y)~x, list(y = as.symbol(nm )))

The key is distinguishing between the character string "yvar" and the
(language) object, as.symbol("yvar").

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, Jan 12, 2016 at 3:10 PM, John Sorkin
<jsor...@grecc.umaryland.edu> wrote:
> I am trying to write a function which will allow me to analyze many variables 
> all of which have the same form, Base.stem and Disch.stem, where stem varies 
> from variable to variable, e.g. Base.rolling and Disch.rolling, 
> Base.standing, Disch.standing.
>
> I want to pass a dataframe and the stem of the name of the pre and post 
> intervention variables. In my function I create the full pre and post 
> intervention Base.stem and Disch.stem variables names (by using the paste 
> function, varpre=paste("Base.",stem,sep="") and 
> varpost=paste("Disch.",stem,sep="")). I also create and add to the dataframe 
> the change variable (i.e. post-pre) formed as 
> ch<-paste("Change.",stem,sep="") and compute the change in the variable:    
> data[,ch]=data[,varpo]-data[,varpre].
>
> If, for example, I run the function doit(rolling, data), the function creates 
> the full pre-intervention variable name Base.rolling, the post-intervention 
> variable name Disch.rolling, and computes Change.rolling = 
> Disch.rolling-Base.rolling. I want to use Change.rolling in a function, but I 
> don't want to have to expressly specify the name of the outcome variable, i.e.
>
>   
> fitwilcox<-wilcox.test(Change.rolling~RANDOMIZED,data=data,na.action=na.omit)
>
> I want to specify the constructed variable, i.e.
>
>  fitwilcox<-wilcox.test(ch~RANDOMIZED,data=data,na.action=na.omit)
>
> This does not work because ch is not in the dataframe, but Change.rolling is. 
> How can I modify the call to the Wilcox.test function so I can specify the 
> change variable constructor. I tried the following but it does not work:
>
>  fitwilcox<-wilcox.test(eval(ch, 
> parent.frame())~RANDOMIZED,data=data,na.action=na.omit)
>
> DATA:
>
>> dput(data2)structure(list(Subj = c(115, 116, 117, 118, 119, 120, 121, 122, 
>> 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 
>> 138, 139, 140, 141, 142, 143, 144, 145, 146, 147),     RANDOMIZED = c(1, 1, 
>> 2, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1, 2,     1, 1, 2, 2, 1, 2, 1, 1, 2, 2, 1, 2, 
>> 1, 2, 2, 2, 2, 2, 2),     Base.rolling = c(0, 6, 6, 3, 3, 1, 4, 6, 4, 6, 4, 
>> 1, 3, NA,     1, 2, 4, 1, 4, 4, 4, 1, 5, 3, 2, 3, 1, 4, 0, 4, 1, NA, 3),     
>> Disch.rolling = c(6, NA, NA, 6, 6, 3, 6, 6, 6, NA, NA, 6,     1, NA, 6, 6, 
>> 6, NA, 6, 6, 6, NA, 4, NA, 6, 6, NA, 6, NA,     6, 6, NA, NA)), .Names = 
>> c("Subj", "RANDOMIZED", "Base.rolling", "Disch.rolling"), class = 
>> "data.frame", row.names = c(NA, -33L))
>
>
> CODE:
>
> doit <- function(var,data){
>   var <-    deparse(substitute(var))
>   print(var)
>   # Create name for basline variable.
>   varpre<-paste("Base.",var,sep="")
>   # Create name for post variable.
>   varpo<-paste("Disch.",var,sep="")
>   # Create a name for the change variable.
>   ch<-paste("Change.",var,sep="")
>   print(ch)
>   # Compute change.
>   data[,ch]=data[,varpo]-data[,varpre]
>
>   cat("\nData used in the analyses\n")
>   print(data[,c("Subj","RANDOMIZED",ch)])
>   # This works when I expressly specify the change variable.
>   cat("This works, expressly specify change variable\n")
>   
> fitwilcox1<-wilcox.test(Change.rolling~RANDOMIZED,data=data,na.action=na.omit)
>   print(fitwilcox1)
>
>   # This does not work.
>   cat("This does NOT work, try to use created change variable.\n")
>   fitwilcox2<-wilcox.test(eval(ch, 
> parent.frame())~RANDOMIZED,data=data,na.action=na.omit)
>   print(fitwilcox2)
>   # Compute wilcoxon statistic.
> }
> doit(rolling,data2)
>
>
> Thank you,
> John
> John David Sorkin M.D., Ph.D.
> Professor of Medicine
> Chief, Biostatistics and Informatics
> University of Maryland School of Medicine Division of Gerontology and 
> Geriatric Medicine
> Baltimore VA Medical Center
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> (Phone) 410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
>
> Confidentiality Statement:
> This email message, including any attachments, is for ...{{dropped:12}}

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to