Subramanian Karthikeyan
2003-Oct-22 17:49 UTC
[R] passing a variable (containing the value of the argument) to a function
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. Thanks, ----- Forwarded by Subramanian Karthikeyan/HC-SC/GC/CA on 2003-10-22 01:47 PM ----- Subramanian Karthikeyan To: r-help at stat.math.ethz.ch cc: 2003-10-22 01:31 Subject: providing a variable as a parameter in a function PM>From a data frame, how do we extract a specific column name, and plug thatinto a command (eg. for Anova as shown below)> df = read.delim("mydata.txt") > y = colnames(df) > r = ncol(x)Lets say that in the data frame column 1 contains treatments, column 2 contains doses, and columns 3, 4, 5 etc. are different responses, and I want to run separate 2-way anovas for each response, i.e. my first anova will be done using col 1: Treatment, col 2: Dose and Col 3: a response, second anova will be done using treatment (col1), dose (col2) and another response (col 4) and so on. I could use a loop to automate the task.> for (i in 3:r) {+ mod = lm(y[i]~Trt*Dose, data = x, contrasts = list(Trt = contr.sum, Dose = contr.sum)) + Anova(mod, type = "III") + } The problem is when I directly plug in y[3] for my response variables name, it gives me an error Error in model.frame(formula, rownames, variables, varnames, extras, extranames, : invalid variable type This is likely because the lm() function wants the actual column name, rather than a variable containing the column name. Can someone advice? Thanks, Karth.
Peter Dalgaard
2003-Oct-22 19:58 UTC
[R] passing a variable (containing the value of the argument) to a function
"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