[R] Referencing a column using a function argument

Eik Vettorazzi E.Vettorazzi at uke.uni-hamburg.de
Wed Jul 27 17:48:40 CEST 2011


Hi,
use tableName[["columnName"]] without quotes, e.g.

tableName[[columnName]] # see ?"[["

and indexing is done via
x<-length(tableName[[columnName]][tableName[[columnName]] > 6] )

but you may consider

x<-sum(tableName[[columnName]]>6)

which saves some time and typing

Hth.

Am 27.07.2011 17:22, schrieb Lin, YunHui:
> Hi all,
> 
> I've been having trouble with something that seems like it should be
> fairly straight forward. Any help at all from more experienced users is
> appreciated!
> 
>  
> 
> I'd like to write a function that uses a column name as an argument.
> However, I run into problems when I try to reference this column within
> the function.
> 
> For example,
> 
>  
> 
> findCutoff <- function(tableName, columnName) {
> 
>  
> 
>                 temporaryTable <- subset(tableName, select = c(V1, V4,
> columnName))
> 
>  
> 
>                 x <- length(tableName$columnName [tableName$columnName >
> 6] )
> 
>  
> 
>                 ...etc.
> 
> }
> 
>  
> 
> I know a couple of ways to reference a column: 
> 
> 1.       tableName$columnName
> 
> 2.       tableName[["columnName"]]
> 
> I tried turning the function arguments into a string like
> "tableName$columnName" to use within my function, but unfortunately that
> didn't work. With the second method I run into problems if I want to
> compute something like x in the example function above.
> 
>  
> 
> Any ideas, suggestions to try?
> 
>  
> 
> Thanks in advance for any responses!
> 
>  
> 
> YunHui Lisa Lin
> Intern, Customer Intelligence & Web Analytics
> 
> Moody's Corporation
> yunhui.lin at moodys.com <mailto:heather.andrews at moodys.com>  
> 
>  
> 
> 
> -----------------------------------------
> The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission. If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited. If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments. Thank you. Every effort is made to keep our network free from viruses. You should, however, review this e-mail message, as well as any attachment thereto, for viruses. We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.


-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790



More information about the R-help mailing list