Tying to underdressed the magic of lm redux

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Tying to underdressed the magic of lm redux

Sorkin, John
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Tying to underdressed the magic of lm redux

Bert Gunter-2
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 <[hidden email]>
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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Tying to underdressed the magic of lm redux

Sorkin, John
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<x-apple-data-detectors://12>
GRECC<x-apple-data-detectors://12> (BT/18/GR)
Baltimore, MD 21201-1524<x-apple-data-detectors://13/0>
(Phone) 410-605-711<tel:410-605-7119>9
(Fax) 410-605-7913<tel:410-605-7913> (Please call phone number above prior to faxing)

On May 29, 2019, at 9:59 PM, Bert Gunter <[hidden email]<mailto:[hidden email]>> 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 <[hidden email]<mailto:[hidden email]>> 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]]

______________________________________________
[hidden email]<mailto:[hidden email]> 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Tying to underdressed the magic of lm redux

Bert Gunter-2
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 <[hidden email]>
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 <[hidden email]> 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 <[hidden email]>
> 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]]
>>
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Tying to underdressed the magic of lm redux

Sorkin, John
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 <[hidden email]>
Sent: Wednesday, May 29, 2019 11:27 PM
To: Sorkin, John
Cc: [hidden email]
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 <[hidden email]<mailto:[hidden email]>> 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<tel:410-605-7119>9
(Fax) 410-605-7913<tel:410-605-7913> (Please call phone number above prior to faxing)

On May 29, 2019, at 9:59 PM, Bert Gunter <[hidden email]<mailto:[hidden email]>> 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 <[hidden email]<mailto:[hidden email]>> 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]]

______________________________________________
[hidden email]<mailto:[hidden email]> 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Tying to underdressed the magic of lm redux

Bert Gunter-2
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 <[hidden email]>
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 <[hidden email]>
> *Sent:* Wednesday, May 29, 2019 11:27 PM
> *To:* Sorkin, John
> *Cc:* [hidden email]
> *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 <[hidden email]>
> 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 <[hidden email]> 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 <[hidden email]>
> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Tying to underdressed the magic of lm redux

Richard O'Keefe-2
In reply to this post by Sorkin, John
You can find the names of the columns of a dataframe using
  colnames(my.df)
A dataframe is a value just as much as a number is, and as such,
doesn't _have_ a name.  However, when you call a function in R,
the arguments are not evaluated, and their forms can be recovered,
just as "plot" does.  In fact, looking at plot to see how it does
that is a good idea.

demo <- function (df) {
   list(name = deparse(substitute(df)), cols = colnames(df))
}

my.df <- data.frame(x = c(1,2), y = c(3,4))

demo(my.df)

$name
[1] "my.df"

$cols
[1] "x" "y"




On Sun, 2 Jun 2019 at 13:43, Sorkin, John <[hidden email]> 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 <[hidden email]>
> Sent: Wednesday, May 29, 2019 11:27 PM
> To: Sorkin, John
> Cc: [hidden email]
> 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 <[hidden email]
> <mailto:[hidden email]>> 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<tel:410-605-7119>9
> (Fax) 410-605-7913<tel:410-605-7913> (Please call phone number above prior
> to faxing)
>
> On May 29, 2019, at 9:59 PM, Bert Gunter <[hidden email]<mailto:
> [hidden email]>> 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 <[hidden email]
> <mailto:[hidden email]>> 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]]
>
> ______________________________________________
> [hidden email]<mailto:[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Tying to underdressed the magic of lm redux

Richard O'Keefe-2
PS:  lm records a copy of the call in its result, but has no other use
for any name the data frame may have had.

On Sun, 2 Jun 2019 at 14:45, Richard O'Keefe <[hidden email]> wrote:

> You can find the names of the columns of a dataframe using
>   colnames(my.df)
> A dataframe is a value just as much as a number is, and as such,
> doesn't _have_ a name.  However, when you call a function in R,
> the arguments are not evaluated, and their forms can be recovered,
> just as "plot" does.  In fact, looking at plot to see how it does
> that is a good idea.
>
> demo <- function (df) {
>    list(name = deparse(substitute(df)), cols = colnames(df))
> }
>
> my.df <- data.frame(x = c(1,2), y = c(3,4))
>
> demo(my.df)
>
> $name
> [1] "my.df"
>
> $cols
> [1] "x" "y"
>
>
>
>
> On Sun, 2 Jun 2019 at 13:43, Sorkin, John <[hidden email]>
> 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 <[hidden email]>
>> Sent: Wednesday, May 29, 2019 11:27 PM
>> To: Sorkin, John
>> Cc: [hidden email]
>> 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 <[hidden email]
>> <mailto:[hidden email]>> 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<tel:410-605-7119>9
>> (Fax) 410-605-7913<tel:410-605-7913> (Please call phone number above
>> prior to faxing)
>>
>> On May 29, 2019, at 9:59 PM, Bert Gunter <[hidden email]<mailto:
>> [hidden email]>> 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 <[hidden email]
>> <mailto:[hidden email]>> 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]]
>>
>> ______________________________________________
>> [hidden email]<mailto:[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Tying to underdressed the magic of lm redux

Berry, Charles
In reply to this post by Sorkin, John
John,

I believe the pieces you are missing are filed under 'computing on the language', 'passing unevaluated objects', and 'language objects'.

Forgive me if I belabor things you already know.  

lm, transform, and many other functions do their "magic" by operating on language objects.

You might find a read of the `Computing on the Language' section in Venables and Pipley's `S Programmming' useful aa background. There are probably many blogs and other on-line resources, too.

The unquoted things you want to operate on are language objects. deparse-ing those objects and trying to operate on character strings is one way to deal with such objects, but it can get messy.  

You might find that `transform.data.frame' provides a useful example of how to deal with language objects provided as arguments to a function.

I suggest you try `debugonce' on lm and on `transform.data.frame'. Run an example of each and inspect each object created along the way.

For lm, you should aim to understand everything through this line:

 mf <- eval(mf, parent.frame())

Getting familiar with `substitute', `match.call', `eval', and friends will help.

HTH,

Chuck

p.s. It sounds like what you want to do is to extend `transform.data.frame' in some way. ??

> On Jun 1, 2019, at 6:43 PM, Sorkin, John <[hidden email]> 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.
>
> T

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Tying to underdressed the magic of lm redux

Uwe Ligges-3
In reply to this post by Sorkin, John
Simply quote the colnames in the call, i.e.:
demo("a", "b", df)

Best,
Uwe Ligges

On 02.06.2019 03:43, Sorkin, John 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 <[hidden email]>
> Sent: Wednesday, May 29, 2019 11:27 PM
> To: Sorkin, John
> Cc: [hidden email]
> 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 <[hidden email]<mailto:[hidden email]>> 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<tel:410-605-7119>9
> (Fax) 410-605-7913<tel:410-605-7913> (Please call phone number above prior to faxing)
>
> On May 29, 2019, at 9:59 PM, Bert Gunter <[hidden email]<mailto:[hidden email]>> 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 <[hidden email]<mailto:[hidden email]>> 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]]
>
> ______________________________________________
> [hidden email]<mailto:[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Tying to underdressed the magic of lm redux

S Ellison-2
In reply to this post by Sorkin, John
If you want to pass a data frame (not just its name) plus (some) column names to your function, the easiest way  is to put the _quoted_ names in a vector, as previously posted. For example

f1 <- function(dfrm, colnames) {
        print(dfrm[,colnames]) #or, if you like, for(nn in colnames) print(dfrm[[nn]])
                                                                #or for(nn in colnames) print(dfrm[,nn])
}
#Using your data frame (with name df to avoid confusion with the density for the F distribution, df())
f1(df1, c("a", "b") ) #c("a", "b") is a character vector.


If you want to avoid quotes and a vector (why?), you could use a one-sided formula
f1f <- function (form, data) { #that way round to look like lm
        print(data[,all.vars(form)])
        #OR (to print one at a time)
        #for(nn in all.vars(form)) print(data[[nn]])
}
f1f(~a+b, df1) #Almost any combination of formula symbols will work but '+' is enough


Finally, some comments on why your code didn't work:
> demo <- function(first,second,df)
        #first and second must exist outside the data frame or the function won't find them and you'll get errors like "object 'b' not found"

>   # None of the following work
>   print(df[,all.vars(first)])
        #all.vars fails because first is not an expression or call object, just a vector (if a exists outside the data frame at all).

>   print(df[,first])
        #Won't work because 'a' is unknown at function call time, and if it did exist this would use it as a vector of names or column numbers.

>   print(df[,"first"])
        #"first" is not a column name in df

>   print(df[,all.vars(second)])
        #all as for 'first' above

>   df[,"sum"] <- print(df[,first])+print(df[,second])
        #Fails principally because df[,first] and df[,second] don't work, for reasons above.
        #It would work if first and second are legitimate index vectors, BUT only because print() returns its arguments invisibly.
        #As a side effect it will print first and second before returning the value.
        # If R didn't return arguments invisibly, you'd be asking R to add together two printouts, which would be
        # about as sensible as asking it to add two pieces of paper together to get a vector of numbers.

It's quicker to say what _does_ work...

First, look up ?"[" and see what kinds of value that takes  and how it works.
It will tell you that row and  col in something like df1[row, col] must be vectors, and that they can be logical, numeric or character.
If they are logical, rows and columns corresponding to TRUE are accessed by df1[row, col]. But their lengths must be equal to the number of rows and columns, respectively, in df1 because TRUE must be at the same location in the vector row (say) as the rows you want from the data frame.
If they are numeric, you'll get the relevant rows and columns counting from top left.
If they are character vectors, you'll get the rows and columns with those names.
Mixing and matching works.

So:
df1[, "a"]
df1[,1]
df1[,c(TRUE, FALSE)]
will all work and return the first column.

and
demo(first="a", second="b", df1)
will work in your function, as would demo("a", "b", df1)

demo(a, b, df1) will NOT work unless a and b are vectors that are legitimate indices for df1

Pretty much nothing else will work either, unless it returns a vecotor that [ can interpret as an index.


There is one more cunning variant of [] for matrices, which is a matrix of indices; I'll leave that for ?"[" to explain.



*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

______________________________________________
[hidden email] 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.