Hi, I am passing a data frame and field name to a function. I've figured out how I can create the formula based on the passed in field name, but I'm struggling to create a vector based in that field. for example if I hard code with the actual field name Y = df$Target, everything works fine. but if I use the passed in parameter name, it doesn't give me what I want, Y = df$mytarget Here is the function, # trying to pass field name to a function logistictest <- function(df,mytarget) { #library for AUC calculation library(caTools) #build logistic model mytarget <- deparse(substitute(mytarget)) myformula <- paste(mytarget," ~ .") myformula <- deparse(substitute(myformula)) logistic_reg <- glm(myformula , data=df, family=binomial(link="logit")) print("model build OK") #score up scores <- predict(logistic_reg, type="response", df) print("model scored OK") #calc AUC Y = df$mytarget auc <- colAUC(scores,Y) print("auc calculated OK") } logistictest(df=trainset,mytarget=Target) [1] "model build OK" [1] "model scored OK" Error in as.vector(x, mode) : invalid 'mode' argument -- View this message in context: http://www.nabble.com/passing-field-name-parameter-to-function-tp25840014p25840014.html Sent from the R help mailing list archive at Nabble.com.
You need to understand how accessing elements of a dataframe happen, especially the '[[' operator (?"[[") x <- function(dataFrame, name) dataFrame[[name]] On Sat, Oct 10, 2009 at 9:27 PM, tdm <philb at philbrierley.com> wrote:> > Hi, > > I am passing a data frame and field name to a function. I've figured out how > I can create the formula based on the passed in field name, but I'm > struggling to create a vector based in that field. > > for example if I hard code with the actual field name > > Y = df$Target, everything works fine. > > but if I use the passed in parameter name, it doesn't give me what I want, > > Y = df$mytarget > > > Here is the function, > > # trying to pass field name to a function > logistictest <- function(df,mytarget) > { > > #library for AUC calculation > library(caTools) > > #build logistic model > mytarget <- deparse(substitute(mytarget)) > myformula <- paste(mytarget," ~ .") > myformula <- deparse(substitute(myformula)) > logistic_reg <- glm(myformula , data=df, family=binomial(link="logit")) > print("model build OK") > > #score up > scores <- predict(logistic_reg, type="response", df) > print("model scored OK") > > #calc AUC > Y = df$mytarget > > auc <- colAUC(scores,Y) > print("auc calculated OK") > > } > > logistictest(df=trainset,mytarget=Target) > > > [1] "model build OK" > [1] "model scored OK" > Error in as.vector(x, mode) : invalid 'mode' argument > -- > View this message in context: http://www.nabble.com/passing-field-name-parameter-to-function-tp25840014p25840014.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
You code is not reproducible. Take a look at this much simpler example: > a x y xx xxx 1 1 4 8 12 2 2 5 9 13 3 3 6 10 14 4 4 7 11 15 5 1 4 8 12 6 2 5 9 13 7 3 6 10 14 8 4 7 11 15 > funct <- function(df, colnm) print(df[, colnm]) > funct(a, "xx") [1] 8 9 10 11 8 9 10 11 #---------- > fortune(106) If the answer is parse() you should usually rethink the question. -- Thomas Lumley R-help (February 2005) #----------- Further simple example: (use dput(a) to produce the structure object:) a <- structure(list(x = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), y = c(4L, 5L, 6L, 7L, 4L, 5L, 6L, 7L), xx = c(8L, 9L, 10L, 11L, 8L, 9L, 10L, 11L), xxx = c(12L, 13L, 14L, 15L, 12L, 13L, 14L, 15L)), .Names = c("x", "y", "xx", "xxx"), row.names = c(NA, -8L), class = "data.frame") lmfunct <- function(df, idvar, dvar) lm( as.formula( paste(dvar, " ~ ", idvar, sep="") ), data=df) lmfunct(a, "x", "y") ####--------------------------- Call: lm(formula = as.formula(paste(dvar, " ~ ", idvar, sep = "")), data = df) Coefficients: (Intercept) x 3 1 -- david On Oct 10, 2009, at 9:27 PM, tdm wrote:> > Hi, > > I am passing a data frame and field name to a function. I've figured > out how > I can create the formula based on the passed in field name, but I'm > struggling to create a vector based in that field. > > for example if I hard code with the actual field name > > Y = df$Target, everything works fine. > > but if I use the passed in parameter name, it doesn't give me what I > want, > > Y = df$mytarget > > > Here is the function, > > # trying to pass field name to a function > logistictest <- function(df,mytarget) > { > > #library for AUC calculation > library(caTools) > > #build logistic model > mytarget <- deparse(substitute(mytarget)) > myformula <- paste(mytarget," ~ .") > myformula <- deparse(substitute(myformula)) > logistic_reg <- glm(myformula , data=df, > family=binomial(link="logit")) > print("model build OK") > > #score up > scores <- predict(logistic_reg, type="response", df) > print("model scored OK") > > #calc AUC > Y = df$mytarget > > auc <- colAUC(scores,Y) > print("auc calculated OK") > > } > > logistictest(df=trainset,mytarget=Target) > > > [1] "model build OK" > [1] "model scored OK" > Error in as.vector(x, mode) : invalid 'mode' argument > -- > View this message in context: http://www.nabble.com/passing-field-name-parameter-to-function-tp25840014p25840014.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
On Oct 10, 2009, at 9:27 PM, tdm wrote:> > Hi, > > I am passing a data frame and field name to a function. I've figured > out how > I can create the formula based on the passed in field name, but I'm > struggling to create a vector based in that field. > > for example if I hard code with the actual field name > > Y = df$Target, everything works fine. > > but if I use the passed in parameter name, it doesn't give me what I > want, > > Y = df$mytarget > > > Here is the function, > > # trying to pass field name to a function > logistictest <- function(df,mytarget) > { > > #library for AUC calculation > library(caTools) > > #build logistic model > mytarget <- deparse(substitute(mytarget)) > myformula <- paste(mytarget," ~ .") > myformula <- deparse(substitute(myformula)) > logistic_reg <- glm(myformula , data=df, > family=binomial(link="logit")) > print("model build OK") > > #score up > scores <- predict(logistic_reg, type="response", df) > print("model scored OK") > > #calc AUC > Y = df$mytarget > > auc <- colAUC(scores,Y) > print("auc calculated OK") > > } > > logistictest(df=trainset,mytarget=Target)A further comment. There is no object "Target" (or if there is, it is created by code that you have not included). There is only a name "Target" for a list element in the object "df". The formalism df $Target is really syntactic window dressing for df$"Target" and it only works because $ is designed to extract named elements from within an object that is visible to to the interpreter at this level. (Sorry for imprecise language, but that what noobs like me end up resorting to.)> > > [1] "model build OK"Actually that is not tested. I suspect that if you wrote: print(logistic_reg) ... it would be empty.> [1] "model scored OK" > Error in as.vector(x, mode) : invalid 'mode' argument > --David Winsemius, MD Heritage Laboratories West Hartford, CT