Jonathan Greenberg
2006-Nov-01 22:06 UTC
[R] Calling an arbitrarily named data frame + variable
If I want to pass a data frame variable name and a specific component name
of the data frame to a function, and I want the function to operate on this
(e.g. Data frame = "data1" and the component in question is
"component1", so
I want the function to operate on data1$component1), how do I pass and call
this info? This would be for an arbitrarily named data frame and component
name, e.g. The function should also be able to handle it if I pass data2 and
component2, e.g. function1 <- function(dataframename,componentname) {} ...
function1("data2","component2")
--j
--
Jonathan A. Greenberg, PhD
NRC Research Associate
NASA Ames Research Center
MS 242-4
Moffett Field, CA 94035-1000
Office: 650-604-5896
Cell: 415-794-5043
AIM: jgrn307
MSN: jgrn307 at hotmail.com
Gabor Grothendieck
2006-Nov-02 01:53 UTC
[R] Calling an arbitrarily named data frame + variable
Try this:
f <- function(DF, vnam) {
if (is.character(DF)) DF <- get(DF)
head(DF[[vnam]])
}
f(iris, "Species")
f("iris", "Species")
On 11/1/06, Jonathan Greenberg <jgreenberg at arc.nasa.gov>
wrote:> If I want to pass a data frame variable name and a specific component name
> of the data frame to a function, and I want the function to operate on this
> (e.g. Data frame = "data1" and the component in question is
"component1", so
> I want the function to operate on data1$component1), how do I pass and call
> this info? This would be for an arbitrarily named data frame and component
> name, e.g. The function should also be able to handle it if I pass data2
and
> component2, e.g. function1 <- function(dataframename,componentname) {}
...
> function1("data2","component2")
>
> --j
>
> --
> Jonathan A. Greenberg, PhD
> NRC Research Associate
> NASA Ames Research Center
> MS 242-4
> Moffett Field, CA 94035-1000
> Office: 650-604-5896
> Cell: 415-794-5043
> AIM: jgrn307
> MSN: jgrn307 at hotmail.com
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>