[R] many chr2factors ?
christian schulz
ozric at web.de
Wed Jun 1 12:01:19 CEST 2005
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
More information about the R-help
mailing list