Alexander.Herr at csiro.au
2007-Aug-31 04:50 UTC
[R] sapply to return factors in dataframe
Hi List, I am trying to randomise a dataframe with mixed factors/numeric variables and return a new (randomised) dataframe with the same columns (as factors/numeric). simgap<-function(x) { ma<-max(x) mi<-min(x) Xout<-runif(length(x),min=mi,max=ma) return(Xout) } rdize.dta<-function(x){ if(is.factor(x)) { cat(str(x),"\n") factor(as.character(round(runif(n=length(x),min=1,max=nlevels(xx)),0)))} else if(is.integer(x)) round(simgap(x),0) else if(is.numeric(x)) simgap(x) else x<-c("Error, not factor/numeric") return(x) } sapply(warpbreaks,FUN=function(x) rdize.dta(x))->test str(test) While I could to this in a loop, I am unable to do this with sapply. Any suggestions? Thanks Herry