[R] sapply to return factors in dataframe

Alexander.Herr at csiro.au Alexander.Herr at csiro.au
Fri Aug 31 06:50:40 CEST 2007


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



More information about the R-help mailing list