[R] Pass Conditional & loop argument into a function
greatest.possible.newbie
daniel.hoop at gmx.net
Wed Aug 8 23:16:29 CEST 2012
Ok I see that point with the quotes. But what I want to do still doesn't
work:
a <- matrix(1:15,ncol=3)
b <- paste( paste("a[," ,paste(1:3), "]",sep="")
,"^",1:3,sep="",collapse="+")
b
#[1] "a[,1]^1+a[,2]^2+a[,3]^3"
#instead of (which I want)
a[,1]^1+a[,2]^2+a[,3]^3
#[1] 1368 1779 2264 2829 3480
or I want to change some function input (dramatically) just by changing two
conditional arguments (in my case constant and c).
a <- matrix(1:15,ncol=3)
b <- matrix(NA,nrow=nrow(a),ncol=ncol(a))
constant <- 1
c <- 5
for (j in 1:ncol(a))
b[,j] <- mapply(function(x){
if(is.null(constant)) {paste(paste("x^", paste(1:c) , sep="", collapse="
+ ") , ", a[,",j,"]" ,sep=" ")
} else if(!is.null(constant)) {paste(paste(constant, paste("x^",
paste(1:c) , sep="",collapse="+"),collapse="+") , ", a[,",j,"]" ,sep=" ") }
})
By the way.. Can anyone tell my why collapse doesn't work in outer paste
function?
--
View this message in context: http://r.789695.n4.nabble.com/Pass-Conditional-loop-argument-into-a-function-tp4639580p4639681.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list