John Sorkin
2016-Dec-05 15:29 UTC
[R] Write a function that allows access to columns of a passed dataframe.
Rui, I appreciate your suggestion, but eliminating the deparse statement does not solve my problem. Do you have any other suggestions? See code below. Thank you, John mydf <- data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) mydf class(mydf) myfun <- function(frame,var){ call <- match.call() print(call) indx <- match(c("frame","var"),names(call),nomatch=0) print(indx) if(indx[1]==0) stop("Function called without sufficient arguments!") cat("I can get the name of the dataframe as a text string!\n") #xx <- deparse(substitute(frame)) print(xx) cat("I can get the name of the column as a text string!\n") #yy <- deparse(substitute(var)) print(yy) # This does not work. print(frame[,var]) # This does not work. print(frame[,"var"]) # This does not work. col <- xx[,"yy"] # Nor does this work. col <- xx[,yy] print(col) } myfun(mydf,age) myfun() 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)>>> Rui Barradas <ruipbarradas at sapo.pt> 12/05/16 10:17 AM >>>Hello, You don't need xx <- deparse(substitute(...)), since you are passing the data.frame to your function. Just use myfun <- function(frame,var){ [...] # Nor does this work. col <- frame[,yy] print(col) } myfun(mydf,age) myfun(frame = mydf, var = age) [1] 2 3 I can get the name of the dataframe as a text string! [1] "mydf" I can get the name of the column as a text string! [1] "age" [1] 20 34 43 32 21 Hope this helps, Rui Barradas Em 05-12-2016 14:44, John Sorkin escreveu:> I am trying to write a function which, when passed the name of a dataframe and the name of a column of the dataframe, will allow me to work on the columns of a dataframe. I can not get my code to work. Please see the code below. Any help in getting the function to work would be appreciated. > > > > > mydf <- data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) > mydf > class(mydf) > > > myfun <- function(frame,var){ > call <- match.call() > print(call) > > > indx <- match(c("frame","var"),names(call),nomatch=0) > print(indx) > if(indx[1]==0) stop("Function called without sufficient arguments!") > > > cat("I can get the name of the dataframe as a text string!\n") > xx <- deparse(substitute(frame)) > print(xx) > > > cat("I can get the name of the column as a text string!\n") > yy <- deparse(substitute(var)) > print(yy) > > > # This does not work. > col <- xx[,"yy"] > > > # Nor does this work. > col <- xx[,yy] > print(col) > } > > > myfun(mydf,age) > > 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) > > > > > 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) > > > > > > Confidentiality Statement: > This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. > ______________________________________________ > R-help at 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. >Confidentiality Statement: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
Bert Gunter
2016-Dec-05 15:36 UTC
[R] Write a function that allows access to columns of a passed dataframe.
John: I think you need to re-read about how functions pass arguments and data frame access works in R. What Rui meant was to get rid of all the xx stuff and access your column by: col <- frame[, yy] That *does* work. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Dec 5, 2016 at 7:29 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote:> Rui, > I appreciate your suggestion, but eliminating the deparse statement does not solve my problem. Do you have any other suggestions? See code below. > Thank you, > John > > > mydf <- data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) > mydf > class(mydf) > > > myfun <- function(frame,var){ > call <- match.call() > print(call) > > > indx <- match(c("frame","var"),names(call),nomatch=0) > print(indx) > if(indx[1]==0) stop("Function called without sufficient arguments!") > > > cat("I can get the name of the dataframe as a text string!\n") > #xx <- deparse(substitute(frame)) > print(xx) > > > cat("I can get the name of the column as a text string!\n") > #yy <- deparse(substitute(var)) > print(yy) > > > # This does not work. > print(frame[,var]) > > > # This does not work. > print(frame[,"var"]) > > > > > # This does not work. > col <- xx[,"yy"] > > > # Nor does this work. > col <- xx[,yy] > print(col) > } > > > myfun(mydf,age) > > > > > myfun() > > > > > > > > > > > > > > > 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) > >>>> Rui Barradas <ruipbarradas at sapo.pt> 12/05/16 10:17 AM >>> > Hello, > > You don't need xx <- deparse(substitute(...)), since you are passing the > data.frame to your function. Just use > > > myfun <- function(frame,var){ > > [...] > > # Nor does this work. > col <- frame[,yy] > print(col) > } > > myfun(mydf,age) > myfun(frame = mydf, var = age) > [1] 2 3 > I can get the name of the dataframe as a text string! > [1] "mydf" > I can get the name of the column as a text string! > [1] "age" > [1] 20 34 43 32 21 > > > Hope this helps, > > Rui Barradas > > > > Em 05-12-2016 14:44, John Sorkin escreveu: >> I am trying to write a function which, when passed the name of a dataframe and the name of a column of the dataframe, will allow me to work on the columns of a dataframe. I can not get my code to work. Please see the code below. Any help in getting the function to work would be appreciated. >> >> >> >> >> mydf <- data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) >> mydf >> class(mydf) >> >> >> myfun <- function(frame,var){ >> call <- match.call() >> print(call) >> >> >> indx <- match(c("frame","var"),names(call),nomatch=0) >> print(indx) >> if(indx[1]==0) stop("Function called without sufficient arguments!") >> >> >> cat("I can get the name of the dataframe as a text string!\n") >> xx <- deparse(substitute(frame)) >> print(xx) >> >> >> cat("I can get the name of the column as a text string!\n") >> yy <- deparse(substitute(var)) >> print(yy) >> >> >> # This does not work. >> col <- xx[,"yy"] >> >> >> # Nor does this work. >> col <- xx[,yy] >> print(col) >> } >> >> >> myfun(mydf,age) >> >> 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) >> >> >> >> >> 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) >> >> >> >> >> >> Confidentiality Statement: >> This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. >> ______________________________________________ >> R-help at 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. >> > > > > Confidentiality Statement: > This email message, including any attachments, is for ...{{dropped:12}}
Rui Barradas
2016-Dec-05 16:15 UTC
[R] Write a function that allows access to columns of a passed dataframe.
Bert is right, that's exactly what I mant. Rui Barradas Em 05-12-2016 15:36, Bert Gunter escreveu:> John: > > I think you need to re-read about how functions pass arguments and > data frame access works in R. > > What Rui meant was to get rid of all the xx stuff and access your column by: > > col <- frame[, yy] > > That *does* work. > > Cheers, > Bert > > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Mon, Dec 5, 2016 at 7:29 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote: >> Rui, >> I appreciate your suggestion, but eliminating the deparse statement does not solve my problem. Do you have any other suggestions? See code below. >> Thank you, >> John >> >> >> mydf <- data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) >> mydf >> class(mydf) >> >> >> myfun <- function(frame,var){ >> call <- match.call() >> print(call) >> >> >> indx <- match(c("frame","var"),names(call),nomatch=0) >> print(indx) >> if(indx[1]==0) stop("Function called without sufficient arguments!") >> >> >> cat("I can get the name of the dataframe as a text string!\n") >> #xx <- deparse(substitute(frame)) >> print(xx) >> >> >> cat("I can get the name of the column as a text string!\n") >> #yy <- deparse(substitute(var)) >> print(yy) >> >> >> # This does not work. >> print(frame[,var]) >> >> >> # This does not work. >> print(frame[,"var"]) >> >> >> >> >> # This does not work. >> col <- xx[,"yy"] >> >> >> # Nor does this work. >> col <- xx[,yy] >> print(col) >> } >> >> >> myfun(mydf,age) >> >> >> >> >> myfun() >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> 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) >> >>>>> Rui Barradas <ruipbarradas at sapo.pt> 12/05/16 10:17 AM >>> >> Hello, >> >> You don't need xx <- deparse(substitute(...)), since you are passing the >> data.frame to your function. Just use >> >> >> myfun <- function(frame,var){ >> >> [...] >> >> # Nor does this work. >> col <- frame[,yy] >> print(col) >> } >> >> myfun(mydf,age) >> myfun(frame = mydf, var = age) >> [1] 2 3 >> I can get the name of the dataframe as a text string! >> [1] "mydf" >> I can get the name of the column as a text string! >> [1] "age" >> [1] 20 34 43 32 21 >> >> >> Hope this helps, >> >> Rui Barradas >> >> >> >> Em 05-12-2016 14:44, John Sorkin escreveu: >>> I am trying to write a function which, when passed the name of a dataframe and the name of a column of the dataframe, will allow me to work on the columns of a dataframe. I can not get my code to work. Please see the code below. Any help in getting the function to work would be appreciated. >>> >>> >>> >>> >>> mydf <- data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) >>> mydf >>> class(mydf) >>> >>> >>> myfun <- function(frame,var){ >>> call <- match.call() >>> print(call) >>> >>> >>> indx <- match(c("frame","var"),names(call),nomatch=0) >>> print(indx) >>> if(indx[1]==0) stop("Function called without sufficient arguments!") >>> >>> >>> cat("I can get the name of the dataframe as a text string!\n") >>> xx <- deparse(substitute(frame)) >>> print(xx) >>> >>> >>> cat("I can get the name of the column as a text string!\n") >>> yy <- deparse(substitute(var)) >>> print(yy) >>> >>> >>> # This does not work. >>> col <- xx[,"yy"] >>> >>> >>> # Nor does this work. >>> col <- xx[,yy] >>> print(col) >>> } >>> >>> >>> myfun(mydf,age) >>> >>> 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) >>> >>> >>> >>> >>> 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) >>> >>> >>> >>> >>> >>> Confidentiality Statement: >>> This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. >>> ______________________________________________ >>> R-help at 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. >>> >> >> >> >> Confidentiality Statement: >> This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. >> ______________________________________________ >> R-help at 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.
Rui Barradas
2016-Dec-05 16:20 UTC
[R] Write a function that allows access to columns of a passed dataframe.
Hello, For some reason I got moderation in reply to Bert's post so I'll retry. Get rid of 'xx', keep 'yy': frame[, yy] # this works Hope this helps, Rui Barradas Em 05-12-2016 15:29, John Sorkin escreveu:> Rui, > I appreciate your suggestion, but eliminating the deparse statement does > not solve my problem. Do you have any other suggestions? See code below. > Thank you, > John > > mydf <- > data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) > mydf > class(mydf) > > myfun <- function(frame,var){ > call <- match.call() > print(call) > > indx <- match(c("frame","var"),names(call),nomatch=0) > print(indx) > if(indx[1]==0) stop("Function called without sufficient arguments!") > > cat("I can get the name of the dataframe as a text string!\n") > #xx <- deparse(substitute(frame)) > print(xx) > > cat("I can get the name of the column as a text string!\n") > #yy <- deparse(substitute(var)) > print(yy) > > # This does not work. > print(frame[,var]) > > # This does not work. > print(frame[,"var"]) > > > # This does not work. > col <- xx[,"yy"] > > # Nor does this work. > col <- xx[,yy] > print(col) > } > > myfun(mydf,age) > > > myfun() > > > > > > > > > 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) > > >>> Rui Barradas <ruipbarradas at sapo.pt> 12/05/16 10:17 AM >>> > Hello, > > You don't need xx <- deparse(substitute(...)), since you are passing the > data.frame to your function. Just use > > > myfun <- function(frame,var){ > > [...] > > # Nor does this work. > col <- frame[,yy] > print(col) > } > > myfun(mydf,age) > myfun(frame = mydf, var = age) > [1] 2 3 > I can get the name of the dataframe as a text string! > [1] "mydf" > I can get the name of the column as a text string! > [1] "age" > [1] 20 34 43 32 21 > > > Hope this helps, > > Rui Barradas > > > > Em 05-12-2016 14:44, John Sorkin escreveu: > > I am trying to write a function which, when passed the name of a > dataframe and the name of a column of the dataframe, will allow me to > work on the columns of a dataframe. I can not get my code to work. > Please see the code below. Any help in getting the function to work > would be appreciated. > > > > > > > > > > mydf <- > data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) > > mydf > > class(mydf) > > > > > > myfun <- function(frame,var){ > > call <- match.call() > > print(call) > > > > > > indx <- match(c("frame","var"),names(call),nomatch=0) > > print(indx) > > if(indx[1]==0) stop("Function called without sufficient arguments!") > > > > > > cat("I can get the name of the dataframe as a text string!\n") > > xx <- deparse(substitute(frame)) > > print(xx) > > > > > > cat("I can get the name of the column as a text string!\n") > > yy <- deparse(substitute(var)) > > print(yy) > > > > > > # This does not work. > > col <- xx[,"yy"] > > > > > > # Nor does this work. > > col <- xx[,yy] > > print(col) > > } > > > > > > myfun(mydf,age) > > > > 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) > > > > > > > > > > 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) > > > > > > > > > > > > Confidentiality Statement: > > This email message, including any attachments, is for the sole use of > the intended recipient(s) and may contain confidential and privileged > information. Any unauthorized use, disclosure or distribution is > prohibited. If you are not the intended recipient, please contact the > sender by reply email and destroy all copies of the original message. > > ______________________________________________ > > R-help at 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. > > > > > _*Confidentiality Statement:*_ > > This email message, including any attachments, is for ...{{dropped:7}}
David Winsemius
2016-Dec-05 17:09 UTC
[R] Write a function that allows access to columns of a passed dataframe.
> On Dec 5, 2016, at 7:29 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote: > > Rui, > I appreciate your suggestion, but eliminating the deparse statement does not solve my problem. Do you have any other suggestions? See code below. > Thank you, > John > > > mydf <- data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) > mydf > class(mydf) > > > myfun <- function(frame,var){ > call <- match.call() > print(call) > > > indx <- match(c("frame","var"),names(call),nomatch=0) > print(indx) > if(indx[1]==0) stop("Function called without sufficient arguments!") > > > cat("I can get the name of the dataframe as a text string!\n") > #xx <- deparse(substitute(frame)) > print(xx) > > > cat("I can get the name of the column as a text string!\n") > #yy <- deparse(substitute(var)) > print(yy) > > > # This does not work. > print(frame[,var]) > > > # This does not work. > print(frame[,"var"]) > > > > > # This does not work. > col <- xx[,"yy"] > > > # Nor does this work. > col <- xx[,yy] > print(col) > } > > > myfun(mydf,age)When you use that calling syntax, the system will supply the values of whatever the `age` variable contains. (And if there is no `age`-named object, you get an error at the time of the call to `myfun`. You need either to call it as: myfun( mydf , "age") # Or: age <- "age" myfun( mydf, age) Unless your value of the `age`-named variable was "age" in the calling environment (and you did not give us that value in either of your postings), you would fail. -- David.> > > > > myfun() > > > > > > > > > > > > > > > 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) > >>>> Rui Barradas <ruipbarradas at sapo.pt> 12/05/16 10:17 AM >>> > Hello, > > You don't need xx <- deparse(substitute(...)), since you are passing the > data.frame to your function. Just use > > > myfun <- function(frame,var){ > > [...] > > # Nor does this work. > col <- frame[,yy] > print(col) > } > > myfun(mydf,age) > myfun(frame = mydf, var = age) > [1] 2 3 > I can get the name of the dataframe as a text string! > [1] "mydf" > I can get the name of the column as a text string! > [1] "age" > [1] 20 34 43 32 21 > > > Hope this helps, > > Rui Barradas > > > > Em 05-12-2016 14:44, John Sorkin escreveu: >> I am trying to write a function which, when passed the name of a dataframe and the name of a column of the dataframe, will allow me to work on the columns of a dataframe. I can not get my code to work. Please see the code below. Any help in getting the function to work would be appreciated. >> >> >> >> >> mydf <- data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) >> mydf >> class(mydf) >> >> >> myfun <- function(frame,var){ >> call <- match.call() >> print(call) >> >> >> indx <- match(c("frame","var"),names(call),nomatch=0) >> print(indx) >> if(indx[1]==0) stop("Function called without sufficient arguments!") >> >> >> cat("I can get the name of the dataframe as a text string!\n") >> xx <- deparse(substitute(frame)) >> print(xx) >> >> >> cat("I can get the name of the column as a text string!\n") >> yy <- deparse(substitute(var)) >> print(yy) >> >> >> # This does not work. >> col <- xx[,"yy"] >> >> >> # Nor does this work. >> col <- xx[,yy] >> print(col) >> } >> >> >> myfun(mydf,age) >> >> 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) >> >> >> >> >> 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) >> >> >> >> >> >> Confidentiality Statement: >> This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. >> ______________________________________________ >> R-help at 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. >> > > > > Confidentiality Statement: > This email message, including any attachments, is for ...{{dropped:15}}
Rui Barradas
2016-Dec-05 17:53 UTC
[R] Write a function that allows access to columns of a passed dataframe.
Hello, Inline. Em 05-12-2016 17:09, David Winsemius escreveu:> >> On Dec 5, 2016, at 7:29 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote: >> >> Rui, >> I appreciate your suggestion, but eliminating the deparse statement does not solve my problem. Do you have any other suggestions? See code below. >> Thank you, >> John >> >> >> mydf <- data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) >> mydf >> class(mydf) >> >> >> myfun <- function(frame,var){ >> call <- match.call() >> print(call) >> >> >> indx <- match(c("frame","var"),names(call),nomatch=0) >> print(indx) >> if(indx[1]==0) stop("Function called without sufficient arguments!") >> >> >> cat("I can get the name of the dataframe as a text string!\n") >> #xx <- deparse(substitute(frame)) >> print(xx) >> >> >> cat("I can get the name of the column as a text string!\n") >> #yy <- deparse(substitute(var)) >> print(yy) >> >> >> # This does not work. >> print(frame[,var]) >> >> >> # This does not work. >> print(frame[,"var"]) >> >> >> >> >> # This does not work. >> col <- xx[,"yy"] >> >> >> # Nor does this work. >> col <- xx[,yy] >> print(col) >> } >> >> >> myfun(mydf,age) > > > When you use that calling syntax, the system will supply the values of whatever the `age` variable contains. (And if there is no `age`-named object, you get an error at the time of the call to `myfun`.Actually, no, which was very surprising to me but John's code worked (not the function, the call). And with the change I've proposed, it worked flawlessly. No errors. Why I don't know. Rui Barradas You need either to call it as:> > myfun( mydf , "age") > > > # Or: > > age <- "age" > myfun( mydf, age) > > Unless your value of the `age`-named variable was "age" in the calling environment (and you did not give us that value in either of your postings), you would fail. >