[R] converting numerical parts of factors into numeric vector

Jim Lemon bitwrit at ozemail.com.au
Fri Apr 28 14:53:04 CEST 2000


This is a bit of a roughie, but it might do what you want.

test<-as.factor(c(1,2,3,4,5,"YES","NO","MAYBE",9,10))
fact2num<-function(f) {
 if(!missing(f)){
  if(is.factor(f)) {
   fnew<-vector("numeric",length(f))
   for(i in 1:length(f)) fnew[i]<-as.numeric(as.character(f[i]))
   return(fnew)
  }
 }
 cat("Usage: fact2num(f)\n\twhere f is a factor vector\n")
}
fact2num(test)

Please loftily ignore the error messages.

Jim

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list