From: Christoph Buser <buser_at_stat.math.ethz.ch>

Date: Thu 02 Jun 2005 - 01:23:03 EST

$ b: chr "a" "b" "c" "d" ...

tofac(dat1)

[1] 2

str(data.frame(dat1))

`data.frame': 10 obs. of 2 variables: $ a: int 1 2 3 4 5 6 7 8 9 10

$ b: chr "a" "b" "c" "d" ...

Christoph Buser <buser@stat.math.ethz.ch> Seminar fuer Statistik, LEO C13

http://stat.ethz.ch/~buser/

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 Thu Jun 02 01:34:30 2005

Date: Thu 02 Jun 2005 - 01:23:03 EST

There are two problems with your code.

First: (and that causes the error) you use in your repeat

if(!is.character(df[,i]))

next

dat1 <- structure(list(a = 1:10, b = letters[1:10]), .Names = c("a", "b"),

row.names = as.character(1:10), class = "data.frame") str(data.frame(dat1)) `data.frame': 10 obs. of 2 variables:$ a: int 1 2 3 4 5 6 7 8 9 10

$ b: chr "a" "b" "c" "d" ...

tofac(dat1)

[1] 2

str(data.frame(dat1))

`data.frame': 10 obs. of 2 variables: $ a: int 1 2 3 4 5 6 7 8 9 10

$ b: chr "a" "b" "c" "d" ...

You can use the following code instead

tofac <- function(x){

for(i in 1:length(x)) {

if(is.character(x[,i]))

x[,i] <- factor(x[,i])

}

x

}

dat1 <- tofac(dat1)

[1] 2

str(dat1)

`data.frame': 10 obs. of 2 variables:
$ a: int 1 2 3 4 5 6 7 8 9 10

$ b: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10

The for loop avoids the problem with the index. Therefore it works in example that have a non character variable in the last column, too and by returning x at the end you are sure that you object keeps existing.

Regards,

Christoph

Christoph Buser <buser@stat.math.ethz.ch> Seminar fuer Statistik, LEO C13

ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-44-632-4673 fax: 632-1228

http://stat.ethz.ch/~buser/

christian schulz writes:

* > 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
*

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 Thu Jun 02 01:34:30 2005

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