Hello R-Helpers, I am not sure if it is a very simple question but I would like to use two (...) in a function, for example, this is a script where I would like to input the variable names (in one of the (...)) and the variances associated to those variables which are not calculated in the script because there is a specific software to calculate it (the other (...)) data <- function ("DAP", "ALT", var1, var2){ #### My wish was to do somethin like function (...,...){ Vec <- matrix(c("DAP", "ALT")) ###here it would came the first (...) Vec <- matrix(c(...)) for (i in seq(along <- Vec)){ caracteristica <- Vec[i] varF <- if (caracteristica == "DAP") var1 else var2 ##here I would like to do something like #if ###caracteristica == variable1) variance1 else if (caracteristica == variable2) variance2 else ... } } but to turn this in a function, I would like to replace ("DAP", "ALT") by any variable and var1, var2 to any variance, and not only necessary 2, for example data ("variable1","variable2","variable3", "variance1", "variance2", "variance3") I am not sure if I made myself clear and if this is an "answerable" doubt, anyway thank you vey much M?rcio -- View this message in context: http://n4.nabble.com/Using-two-in-a-function-tp932200p932200.html Sent from the R help mailing list archive at Nabble.com.
isn't it simpler just to pass two vectors, say v1 and v2, in which one contains the object names and the other has the associated variances? (btw, "data" isn't a good function name) myData <- function(v1, v2){ Vec <- matrix(v1) varF <- v2 } I may have misunderstood your question, but IMHO all you need is to guarantee that v1 and v2 are properly aligned. Cheers, b On Dec 1, 2009, at 11:30 AM, M?rcio Resende wrote:> > Hello R-Helpers, > I am not sure if it is a very simple question but I would like to use two > (...) in a function, for example, > > this is a script where I would like to input the variable names (in one of > the (...)) and the variances associated to those variables which are not > calculated in the script because there is a specific software to calculate > it (the other (...)) > > data <- function ("DAP", "ALT", var1, var2){ #### My wish was to do > somethin like function (...,...){ > Vec <- matrix(c("DAP", "ALT")) ###here it would came the first (...) Vec > <- matrix(c(...)) > for (i in seq(along <- Vec)){ > caracteristica <- Vec[i] > > varF <- if (caracteristica == "DAP") var1 else var2 ##here I would like > to do something like #if ###caracteristica == variable1) variance1 else if > (caracteristica == variable2) variance2 else ... > } > } > > but to turn this in a function, I would like to replace ("DAP", "ALT") by > any variable and var1, var2 to any variance, and not only necessary 2, for > example > data ("variable1","variable2","variable3", "variance1", "variance2", > "variance3") > > I am not sure if I made myself clear and if this is an "answerable" doubt, > anyway thank you vey much > M?rcio > -- > View this message in context: http://n4.nabble.com/Using-two-in-a-function-tp932200p932200.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Seems pretty doubtful that a construction like (..., ...) is going to be accepted by the R interpreter. However, if you construct your function to handle two objects of equal length, one for the variable names and the other for their variances, there should be no problem. If yu want to handle a more general situation where you are passing an arbitrary list and want the first half interpreted as variable names and the second half interpreted as variances, well that can be programmed as well. On Dec 1, 2009, at 8:30 AM, M?rcio Resende wrote:> > Hello R-Helpers, > I am not sure if it is a very simple question but I would like to > use two > (...) in a function, for example, > > this is a script where I would like to input the variable names (in > one of > the (...)) and the variances associated to those variables which are > not > calculated in the script because there is a specific software to > calculate > it (the other (...)) > > data <- function ("DAP", "ALT", var1, var2){ #### My wish was to do > somethin like function (...,...){ > Vec <- matrix(c("DAP", "ALT")) ###here it would came the first > (...) Vec > <- matrix(c(...)) > for (i in seq(along <- Vec)){ > caracteristica <- Vec[i] > > varF <- if (caracteristica == "DAP") var1 else var2 ##here I > would like > to do something like #if ###caracteristica == variable1) variance1 > else if > (caracteristica == variable2) variance2 else ... > } > }At this point, because of the bizarrely placed comment symbols, I am wondering if our mail clients are using the same end-of-line markers.> > but to turn this in a function, I would like to replace ("DAP", > "ALT") by > any variable and var1, var2 to any variance, and not only necessary > 2, for > example > data ("variable1","variable2","variable3", "variance1", "variance2", > "variance3") > > I am not sure if I made myself clear and if this is an "answerable" > doubt, > anyway thank you vey much > M?rcio > --David Winsemius, MD Heritage Laboratories West Hartford, CT
Just pass a named vector f <- function(vars) { nms <- names(vec); ... } f(c(DAP = var1, ALT = var2)) 2009/12/1 Márcio Resende <mresendeufv@yahoo.com.br>> > Hello R-Helpers, > I am not sure if it is a very simple question but I would like to use two > (...) in a function, for example, > > this is a script where I would like to input the variable names (in one of > the (...)) and the variances associated to those variables which are not > calculated in the script because there is a specific software to calculate > it (the other (...)) > > data <- function ("DAP", "ALT", var1, var2){ #### My wish was to do > somethin like function (...,...){ > Vec <- matrix(c("DAP", "ALT")) ###here it would came the first (...) Vec > <- matrix(c(...)) > for (i in seq(along <- Vec)){ > caracteristica <- Vec[i] > > varF <- if (caracteristica == "DAP") var1 else var2 ##here I would like > to do something like #if ###caracteristica == variable1) variance1 else > if > (caracteristica == variable2) variance2 else ... > } > } > > but to turn this in a function, I would like to replace ("DAP", "ALT") by > any variable and var1, var2 to any variance, and not only necessary 2, for > example > data ("variable1","variable2","variable3", "variance1", "variance2", > "variance3") > > I am not sure if I made myself clear and if this is an "answerable" doubt, > anyway thank you vey much > Márcio > -- > View this message in context: > http://n4.nabble.com/Using-two-in-a-function-tp932200p932200.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]