On Dec 30, 2010, at 07:12 , <bill.venab...@csiro.au> <bill.venab...@csiro.au> wrote:
> Here is an alternaive Now that's a rather interesting novel word you got there, Bill.... > approach that is closer to that used by lm and friends. > >> df <- data.frame(x=1:10,y=11:20) >> test <- function(col, dat) eval(substitute(col), envir = dat) >> test(x, df) > [1] 1 2 3 4 5 6 7 8 9 10 >> test(y, df) > [1] 11 12 13 14 15 16 17 18 19 20 >> > > There is a slight added bonus this way > >> test(x+y+1, df) > [1] 13 15 17 19 21 23 25 27 29 31 >> > > (Well, I did say 'slight'.) > > Bill Venables. > Yes, and similar stuff goes on in subset.data.frame(), with(), etc. However, there is a trend towards preferring to do non-standard evaluation in a more standard (less non-standard?) way, using formulas: > test <- function(f, dat) eval(f[[2]], envir = dat, enclos=environment(f)) > df <- data.frame(x=1:10,y=11:20) > test(~x,df) [1] 1 2 3 4 5 6 7 8 9 10 > test(~x+y,df) [1] 12 14 16 18 20 22 24 26 28 30 Or, you could pass expression(x) or quote(x) explicitly, but it's a bit more long-winded and you lose the ability to pass along the environment of the formula (and typically use enclos=parent.frame() instead). > > ________________________________________ > From: r-help-boun...@r-project.org [r-help-boun...@r-project.org] On Behalf > Of David Winsemius [dwinsem...@comcast.net] > Sent: 30 December 2010 10:44 > To: John Sorkin > Cc: r-help@r-project.org > Subject: Re: [R] access a column of a dataframe without qualifying the name > of the column > > On Dec 29, 2010, at 7:11 PM, John Sorkin wrote: > >> I am trying to write a function that will access a column of a data >> frame without having to qualify the name of the data frame column as >> long as the name of the dataframe is passed to the function. As can >> be seen from the code below, my function is not working: > > Not sure what the verb "qualify" means in programming. Quoting? > >> >> df <- data.frame(x=1:10,y=11:20) >> df >> >> test <- function(column,data) { >> print(data$column) >> } >> >> test(x,df) >> >> I am trying to model my function after the way that lm works where >> one needs not qualify column names, i.e. > > >> df <- data.frame(x=1:10,y=11:20) >> test <- function(column,dat) { print(colname <- > deparse(substitute(column))) > + dat[[colname]] > + } >> >> test(x,df) > [1] "x" > [1] 1 2 3 4 5 6 7 8 9 10 >> > > -- > David. > > >> >> >> fit1<- lm(y~x,data=df) >> >> >> John David Sorkin M.D., Ph.D. >> Chief, Biostatistics and Informatics >> University of Maryland School of Medicine Division of Gerontology >> Baltimore VA Medical Center >> 10 North Greene Street >> GRECC (BT/18/GR) >> Baltimore, MD 21201-1524 >> (Phone) 410-605-7119 >> (Fax) 410-605-7913 (Please call phone number above prior to faxing) >> >> Confidentiality Statement: >> This email message, including any attachments, is for th...{{dropped: >> 6}} >> >> ______________________________________________ >> R-help@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 > West Hartford, CT > > ______________________________________________ > R-help@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. > > ______________________________________________ > R-help@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. -- Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd....@cbs.dk Priv: pda...@gmail.com ______________________________________________ R-help@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.