[R] many chr2factors ?

From: christian schulz <ozric_at_web.de>
Date: Wed 01 Jun 2005 - 20:01:19 EST


Hi,

i would like transfrom
characters from a data.frame to factors automatic.

 > tofac <- function(df){
+ i=0
+ repeat{
+ i <- i+1
+ if(!is.character(df[,i]))
+ next
+ df[,i] <- as.factor(df[,i])
+ print(i)
+ if(i == length(df))
+ break }
+ }

 >
 > tofac(abrdat)

[1] 7
[1] 8
[1] 9
[1] 11
[1] 13
[1] 15

Error in "[.data.frame"(df, , i) : undefined columns selected

This are the correct columns and i get the idea put into the loop a empty matrix with dimension like df and return it!?

Another check?
abrdat2 <- apply(abrdat,2,function(x)
ifelse(is.character(x),as.factor(x),x))

many thanks & regards,
christian



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jun 01 20:33:32 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:20 EST