AMB
AMB

Reputation: 139

Using an argument to define an extraction operator in user defined function

I have created a function that uses the subset function. How do I assign an argument then have it used after the extraction operator?

Here is what I have now:

function_test<-function(time1,size,Param){ test1_in_equilibrium<-(subset(alldata,Time>=time1 & FinalPopSize==size)$Param) }

Given the following call: function_test(100,5000,Time) I would like R to expand it like so: test1_in_equilibrium<-(subset(alldata,Time>=time1 & FinalPopSize==size)$Param)

Unfortunately when I attempt to run this function I receive the error object "Time" not found.

I assume I am missing an escape character or something similar but have been unable to find it.

Thanks!

Upvotes: 0

Views: 38

Answers (1)

Simon
Simon

Reputation: 602

You cannot add the $ operator to a function call and you cannot use a variable with the $ operator.

However, I understand that you want to get the column defined by the input variable Param from the subsetted data.frame. In this case you can easily write the function like this:

function_test <- function(time1,size,Param){
  reduced_data <- subset(alldata, Time>=time1 & FinalPopSize==size)
  test1_in_equilibrium <- reduced_data[, Param]
}

Upvotes: 2

Related Questions