[R] access a column of a dataframe without qualifying the name of the column

John Sorkin jsorkin at grecc.umaryland.edu
Thu Dec 30 03:24:49 CET 2010


Thank you
John




John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
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)>>> Bert Gunter <gunter.berton at gene.com> 12/29/2010 8:17 PM >>>
?substitute

test <- function(col,frm) {
  eval(substitute(col),frm)
}

test2 <- function(col,frm){
  cname<- deparse(substitute(col))
  frm[[cname]]
}

 z <- data.frame(x=1:3,y=letters[1:3])

test(x, z)

test2(x, z)


-- Bert

On Wed, Dec 29, 2010 at 4:44 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Dec 29, 2010, at 7:11 PM, John Sorkin wrote:
>
>> I am trying to write a function that will access a column of a data frame
>> without having to qualify the name of the data frame column as long as the
>> name of the dataframe is passed to the function. As can be seen from the
>> code below, my function is not working:
>
> Not sure what the verb "qualify" means in programming. Quoting?
>
>>
>> df <- data.frame(x=1:10,y=11:20)
>> df
>>
>> test <- function(column,data) {
>>  print(data$column)
>> }
>>
>> test(x,df)
>>
>> I am trying to model my function after the way that lm works where one
>> needs not qualify column names, i.e.
>
>
>> df <- data.frame(x=1:10,y=11:20)
>> test <- function(column,dat) { print(colname <-
>> deparse(substitute(column)))
> +  dat[[colname]]
> + }
>>
>> test(x,df)
> [1] "x"
>  [1]  1  2  3  4  5  6  7  8  9 10
>>
>
> --
> David.
>
>
>>
>>
>> fit1<- lm(y~x,data=df)
>>
>>
>> John David Sorkin M.D., Ph.D.
>> Chief, Biostatistics and Informatics
>> University of Maryland School of Medicine Division of Gerontology
>> 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 th...{{dropped:6}}
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
Bert Gunter
Genentech Nonclinical Biostatistics

Confidentiality Statement:
This email message, including any attachments, is for th...{{dropped:6}}



More information about the R-help mailing list