[R] Extracting factor columns from a data frame

Serge Merzliakov smerzlia at optusnet.com.au
Thu Apr 24 14:14:53 CEST 2008


Hi All,
    I have attempted to extract only the factor columns from  an 
existing data set inside a loop without success . I tried the transform 
function which worked, but not inside the loop (attempts with cbind did 
not work either - inside a loop). Here is my function:

getcatcolumns<-function(x)
{
    # ignore type checking for data frames etc
     res <- data.frame()
     for(i in 1:length(x))
     {
        if (class(x[,i]) =="factor") res <- transform(res, colname = 
x[,i])  # append the factor column to the new data frame (res)
     }
   return(res)
}

The error I get is: "arguments imply differing number of rows: 0, 5000"

All help appreciated.
 
Regards,
 Serge Merzliakov



More information about the R-help mailing list