[R] passing a variable (containing the value of the argument) to a function
Spencer Graves
spencer.graves at pdf.com
Wed Oct 22 22:23:28 CEST 2003
Hi, Peter:
How does that compare with the following:
for (myname in names(myframe)[1:4]){
mdl <- formula(paste(myname, "~ etc.etc"))
myfit <- lm(mdl, data=myframe)
print(summary(myfit))
}
Or:
for (myname in names(myframe)[1:4]){
lm.txt <- paste("lm(", myname, "~ etc.etc, data=myframe)")
myfit <- eval(parse(text=lm.txt))
print(summary(myfit))
}
You are teaching me new uses of "substitute", and I just wonder
about the relative advantages and disadvantages of the different
approaches.
Thanks,
spencer graves
Peter Dalgaard wrote:
>"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))
>}
>
>
>
More information about the R-help
mailing list