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
Henrique Dallazuanna
2008-Apr-24 14:25 UTC
[R] Extracting factor columns from a data frame
Try this: dat <- data.frame(x = gl(4, 5), y = gl(5, 4), z = rnorm(20)) res <- dat[sapply(dat, is.factor)] On Thu, Apr 24, 2008 at 9:14 AM, Serge Merzliakov <smerzlia@optusnet.com.au> wrote:> 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 > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]