Hello all, I'm having a problem concerning choosing columns from a dataset in a function. I'm writing a function for data input etc., which first reads the data, and then does several data manipulation tasks. The function can be then used, with just giving the path of the .txt file where the data is being held. These datasets consists of over 20 different analytes. Though, statistical analyses should be made seperately analyte by analyte. So the function needs to be able to choose a certain analyte based on what the user of the function gives as a parameter when calling the function. The name of the analyte user gives, is the same as a name of a column in the data set. The question is: how can I refer to the parameter which the user gives, inside the function? I cannot give the name of the analyte directly inside the function, as the same function should work for all the 20 analytes. I'm giving some code for clarification:>datainput <- function(data1,data2,data3,data4,data5,data6,analyte) >{... ##data1-data6 being the paths of the six datasets I want to combine and analyte being the special analyte I want to analyze and which can be found on each of the datasets as a columnname.## ##Then:## ...>data.whole <- subset(data.whole,select=c(Sample.Name,Analyte.Values,Day,Plate)) ##Is for choosing the columns needed for analysis. The "Analyte" should now be the column of the analyte, the users is referring to when calling the datainput-function. How to do it? ## I've tried something like>data.whole$Analyte.Values <- data.whole$analyte ##(Or in quotes"analyte") But this does not work. I've tried several other "tricks" also, but cannot get it to work. Can someone help? Thanks in advance, Jouni
Adaikalavan Ramasamy
2007-May-31 13:56 UTC
[R] Choosing a column for analysis in a function
Perhaps the use of as.character() like following might help? data.whole$Analyte.Values <- data.whole$as.character(analyte) Junnila, Jouni wrote:> Hello all, > > I'm having a problem concerning choosing columns from a dataset in a > function. > > I'm writing a function for data input etc., which first reads the data, > and then does several data manipulation tasks. > The function can be then used, with just giving the path of the .txt > file where the data is being held. > > These datasets consists of over 20 different analytes. Though, > statistical analyses should be made seperately analyte by analyte. So > the function needs to be able to choose a certain analyte based on what > the user of the function gives as a parameter when calling the function. > The name of the analyte user gives, is the same as a name of a column in > the data set. > > The question is: how can I refer to the parameter which the user gives, > inside the function? I cannot give the name of the analyte directly > inside the function, as the same function should work for all the 20 > analytes. > I'm giving some code for clarification: > >> datainput <- function(data1,data2,data3,data4,data5,data6,analyte) >> { > ... > ##data1-data6 being the paths of the six datasets I want to combine and > analyte being the special analyte I want to analyze and which can be > found on each of the datasets as a columnname.## > ##Then:## > ... >> data.whole <- subset(data.whole, > select=c(Sample.Name,Analyte.Values,Day,Plate)) > > ##Is for choosing the columns needed for analysis. The "Analyte" should > now be the column of the analyte, the users is referring to when calling > the datainput-function. How to do it? ## > I've tried something like >> data.whole$Analyte.Values <- data.whole$analyte ##(Or in quotes > "analyte") > But this does not work. I've tried several other "tricks" also, but > cannot get it to work. Can someone help? > > Thanks in advance, > > Jouni > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > >