[R] passing a variable (containing the value of the argument) to a function

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Oct 22 21:58:36 CEST 2003


"Subramanian Karthikeyan" <Subramanian_Karthikeyan at hc-sc.gc.ca> writes:

> My previous question put in a simpler  way:
> 
> How would I pass a value of a variable to a function such as
> 
> lm(Effect1~Trt*Dose, data = x, contrasts = list(Trt = contr.sum, Dose =
> contr.sum))?
> 
> Here, 'Effect' is a column name in my data matrix, and I want "Effect1" to
> be replaced by "Effect2" and so on (my other column names in the data
> frame) for successive anova calculations. So I am storing the column names
> as an array, and passing the array as a parameter to the lm() function.

A canonical trick is

for (myname in names(myframe)){
  mycall <- substitute(lm(myvar~etc.etc.....),list(myvar=as.name(myname)))
  myfit <- eval(mycall)
  print(summary(myfit))
}

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list