[R] Tying to underdressed the magic of lm redux

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Thu May 30 05:27:13 CEST 2019


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 <jsorkin using 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.4567 using 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 <jsorkin using 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 using 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]]



More information about the R-help mailing list