[R] Pass Conditional & loop argument into a function

greatest.possible.newbie daniel.hoop at gmx.net
Wed Aug 8 14:44:17 CEST 2012


Dear R community,

I have been spending hours to solve my problem myself but I'm just unable to
do it. So excuse me if I demand your time.
My problem is, that inside my function, I want to pass conditional arguments
(that are the result of a loop) into a function.
I am sure that my approach is kind of stupid but I cannot think of another
way to do it. I apologize...

foo <- function(..., degree=..){
  degree<-3 # example
  
  conditional.argument <- rep(NA,degree)
  for(i in 1:(degree-1)) conditional.argument[i] <- paste("X^",i,"+",sep="")
  conditional.argument[degree] <- paste("X^",degree,sep="")
  conditional.argument <- paste(conditional.argument,collapse="")
  
  conditional.argument
#  "X^1+X^2+X^3"

  fit <- lm(Y~conditional.argument)
  ...
  return(fit)
}

I know that there is the poly() function which I am not looking for.
I am having this problem as well when I want to do a boxplot with variable
number of groups to compare.

multiple.boxplot <- function(...,nvariables){
conditional.argument <- data[,1], data[,2], ...,data[,nvariables]          #
should look like that in the end
boxplot(conditional.argument)
}

Can anyone give me a hint?
Daniel Hoop




--
View this message in context: http://r.789695.n4.nabble.com/Pass-Conditional-loop-argument-into-a-function-tp4639580.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list