Hint: > all.vars(a) Error in all.vars(a) : object 'a' not found > all.vars(quote(a)) ## protects "a" from evaluation; quote(a) is a symbol expression [1] "a"
> all.vars(~a) ## a formula expression [1] "a" -- Bert On Sat, Jun 1, 2019 at 6:43 PM Sorkin, John <jsor...@som.umaryland.edu> wrote: > Colleagues, > > Despite Bert having tried to help me, I am still unable to perform a > simple act with a function. I want to pass the names of the columns of a > dataframe along with the name of the dataframe, and use the parameters to > allow the function to access the dataframe and modify its contents. > > I apologize multiple postings regarding this question, but it is a > fundamental concept that one who wants to program in R needs to know. > > Thank you, > John > > # Create a toy dataframe. > df <- data.frame(a=c(1:20),b=(20:39)) > df > > > # Set up a function that will access the first and second columns of the > # data frame, print the columns of the dataframe and add the columns > demo <- function(first,second,df) > { > # None of the following work > print(df[,all.vars(first)]) > print(df[,first]) > print(df[,"first"]) > > print(df[,all.vars(second)]) > print(df[,second]) > print(df[,"second"]) > > df[,"sum"] <- print(df[,first])+print(df[,second]) > > } > demo(a,b, df) > > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > 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) > > > > ------------------------------ > *From:* Bert Gunter <bgunter.4...@gmail.com> > *Sent:* Wednesday, May 29, 2019 11:27 PM > *To:* Sorkin, John > *Cc:* r-help@r-project.org > *Subject:* Re: [R] Tying to underdressed the magic of lm redux > > Depends on how you want to specify variables. You are not clear (to me) on > this. But, for instance: > > demo <- function(form,df) > { > av <- all.vars(form) > df[,av] > } > demo(~a+b, df) > demo(a~b,df) > > ?all.vars, ?all.names for details > > Bert Gunter > > > On Wed, May 29, 2019 at 7:33 PM Sorkin, John <jsor...@som.umaryland.edu> > wrote: > > Bert, > Thank you for your reply. You are correct that your code will print the > contents of the data frame. While it works, it is not as elegant as the lm > function. One does not have to pass the independent and dependent variables > to lm In parentheses. > > Fit1<-lm(y~x,data=mydata) > > None of the parameters to lm are passed in quotation marks. Somehow, using > deparse(substitute()) and other magic lm is able to get the data in the > dataframe mydata. I want to be able to do the same magic in functions I > write; pass a dataframe and column names, all without quotation marks and > be able to write code that will provide access to the columns of the > dataframe without having to pass the column names in quotation marks. > Thank you, > John > > John David Sorkin M.D., Ph.D. > > Professor of Medicine > > Chief, Biostatistics and Informatics > > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > > Baltimore VA Medical Center > > 10 North Greene Street > > GRECC (BT/18/GR) > > Baltimore, MD 21201-1524 > > (Phone) 410-605-711 <410-605-7119>9 > > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > On May 29, 2019, at 9:59 PM, Bert Gunter <bgunter.4...@gmail.com> wrote: > > Basically, huh? > > > df <- data.frame(a = 1:3, b = letters[1:3]) > > nm <- names(df) > > print(df[,nm[1]]) > [1] 1 2 3 > > print(df[,nm[2]]) > [1] a b c > Levels: a b c > > This can be done within a function, of course: > > > demo <- function(df, colnames){ > + print(df[,colnames]) > + } > > demo(df,c("a","b")) > a b > 1 1 a > 2 2 b > 3 3 c > > Am I missing something? (Apologies, if so). > > Bert Gunter > > > > On Wed, May 29, 2019 at 6:40 PM Sorkin, John <jsor...@som.umaryland.edu> > wrote: > > Thanks to several kind people, I understand how to use > deparse(substitute(paramter)) to get as text strings the arguments passed > to an R function. What I still can't do is put the text strings recovered > by deparse(substitute(parameter)) back together to get the columns of a > dataframe passed to the function. What I want to do is pass a column name > to a function along with the name of the dataframe and then, within the > function access the column of the dataframe. > > I want the function below to print the columns of the dataframe testdata, > i.e. testdata[,"FSG"] and testdata[,"GCM"]. I have tried several ways to > tell the function to print the columns; none of them work. > > I thank everyone who has helped in the past, and those people who will > help me now! > > John > > testdata <- structure(list(FSG = c(271L, 288L, 269L, 297L, 311L, 217L, > 235L, > > 172L, 201L, 162L), CGM = c(205L, 273L, > 226L, 235L, 311L, 201L, > > 203L, 155L, 182L, 163L)), row.names = > c(NA, 10L), class = "data.frame") > > cat("This is the data frame") > > class(testdata) > > testdata > > > > BAPlot <- function(first,second,indata){ > > # these lines of code work > > col1 <- deparse(substitute(first)) > > col2 <- deparse(substitute(second)) > > thedata <- deparse(substitute(third)) > > print(col1) > > print(col2) > > print(thedata) > > cat("This gets the data, but not as a dataframe\n") > > zoop<-paste(indata) > > print(zoop) > > cat("End This gets the data, but not as a dataframe\n") > > # these lines do not work > > print(indata[,first]) > > print(indata[,"first"]) > > print(thedata[,col1]) > > paste(zoop[,paste(first)]) > > paste(zoop[,first]) > > zap<-paste(first) > > print(zap) > > } > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.