Subramanian Karthikeyan
2003-Oct-22 17:31 UTC
[R] providing a variable as a parameter in a function
>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.
Dear Subramanian, How about this: for (y in df[, 3:5]) { mod = lm(y ~ Trt*Dose, data = x, contrasts = list(Trt = contr.sum, Dose = contr.sum)) Anova(mod, type = "III") } Does that give you what you want? John At 01:31 PM 10/22/2003 -0400, Subramanian Karthikeyan wrote:> >From a data frame, how do we extract a specific column name, and plug that >into 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. > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help____________________________ John Fox Department of Sociology McMaster University email: jfox at mcmaster.ca web: http://www.socsci.mcmaster.ca/jfox
Subramanian Karthikeyan
2003-Oct-22 19:15 UTC
[R] providing a variable as a parameter in a function
Thanks John, Your solution worked.. After i posted my message I tried sth else which also worked... I tried this: for (i in 3:cnum) { fla = as.formula(paste((cnom[i],"~","Trt*Dose")) mod = lm(fla, data = x, contrasts = list(Trt = contr.sum, Dose contr.sum)) an = Anova(mod,type = "III") print(an) } but yours is a succint and nice solution. Thanks again, Karth. John Fox <jfox at mcmaster.ca To: "Subramanian Karthikeyan" <Subramanian_Karthikeyan at hc-sc.gc.ca> > cc: r-help at stat.math.ethz.ch Subject: Re: [R] providing a variable as a parameter in a function 2003-10-22 02:24 PM Dear Subramanian, How about this: for (y in df[, 3:5]) { mod = lm(y ~ Trt*Dose, data = x, contrasts = list(Trt contr.sum, Dose = contr.sum)) Anova(mod, type = "III") } Does that give you what you want? John At 01:31 PM 10/22/2003 -0400, Subramanian Karthikeyan wrote:> >From a data frame, how do we extract a specific column name, and plugthat>into 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 variablesname,>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. > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help____________________________ John Fox Department of Sociology McMaster University email: jfox at mcmaster.ca web: http://www.socsci.mcmaster.ca/jfox