I forgot to say that I've commented out the line
# This does not work.
#col <- xx[,"yy"]
Rui Barradas
Em 05-12-2016 15:17, Rui Barradas escreveu:
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 ...{{dropped:8}}
______________________________________________
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.
______________________________________________
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.