I have a R code that looks something like this:
StatFunction <- function(CSVImport, AnimalID_i,Treatment_i){
#Data Input from a CSV file (example below)
Data <- read.csv(CSVImport)
All goes well so far, as the data is read off the csv file. I am calling the function like this
StatFunction (path/to/file/, "Animal", "Treatment")
Note that the input arguments are strings representing the column name of the CSV. Now, I try to sort out the data frame like this:
#Trying to sort out the input
Data <- within(Data, {
FinalID <- (factor(as.character(FinalID_i)))
AnimalID <- (factor(as.character(AnimalID_i)))
This doesn't work with the argument of the StatFunction. It gives me this
[1] "---Final ID---"
[1] Treatment
Levels: Treatment
[1] "---Animal ID---"
[1] Animal
Levels: Animal
When, Substituting the arguments directly into the code, like this
FinalID <- (factor(Treatment))
AnimalID <- (factor(Animal))
It gives me the expected result.
[1] "---Final ID---"
[1] 0 0 1 1 1
Levels: 0 1
[1] "---Animal ID---"
[1] 722 723 724 727 728
Levels: 722 723 724 727 728
So my question is how to make this work when passing arguments and not burning the names in the code?