Skip to content
Prev 157331 / 398506 Next

data frame column name as a function argument

First - you need to pass the data frame into the function.

testing <- function (d, colname) {
  return (d[[colname]])
}

d <- data.frame(cbind(x=1, y=1:10))

print (testing(d, 'x')) 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of eric lee
Sent: Friday, September 26, 2008 3:10 PM
To: r-help at r-project.org
Subject: [R] data frame column name as a function argument

Hello,

I'd like to pass a column name as the argument for a function, but I'm
getting "NULL" as a return value.  Any suggestions?  Thanks.
x  y
1  1  1
2  1  2
3  1  3
4  1  4
5  1  5
6  1  6
7  1  7
8  1  8
9  1  9
10 1 10
+ tst <- d$var[3]
+ tst
+ }
NULL


______________________________________________
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.