Re: [R] Extracting factor columns from a data frame

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Thu, 24 Apr 2008 11:25:40 -0300

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_at_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_at_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]]


______________________________________________ R-help_at_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.

Received on Thu 24 Apr 2008 - 14:34:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Apr 2008 - 15:00:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive