[R] How to combine matrix with sapply function?

Peng Yu pengyu.ut at gmail.com
Sat Nov 28 02:19:52 CET 2009


I want an 'apply' function that can give me results by bind the result
from each function call of g. Could somebody let me know how to do it?

g<-function(x){
  if(x==1){
    rbind(c(1,3,8))
  } else if(x==2) {
    rbind(c(4,7,2), c(3,7,3)) #if I use line, sapply() below gives me a list
    #rbind(c(4,7,2)) #if I use this line, sapply() below gives me a matrix
  } else if(x==3) {
    rbind(c(4,3,1))
  }
}

x=1:3

sapply(x,g)




More information about the R-help mailing list