[R] forcing apply() to return data frame

Marc Schwartz (via MN) mschwartz at mn.rr.com
Fri Apr 21 17:25:11 CEST 2006


On Fri, 2006-04-21 at 07:37 -0700, Thomas Lumley wrote:
> On Fri, 21 Apr 2006, Federico Calboli wrote:
> 
> > Hi All,
> >
> > I am (almost) successfully using apply() to apply a function recursively
> > on a data matrix. The function is question is as.genotype() from the
> > library 'genetics'
> >
> > apply(subset(chr1, names$breed == 'lab'),2,as.genotype,sep ="")
> >
> > Unfortuantely apply puts it's results into a matrix object rather than a
> > data frame, tranforming my factors into numerics and making the results
> > useless.
> >
> > Is there a way of forcing apply() to return a data frame rather than a
> > matrix?
> >
> 
> The conversion to a matrix happens on the way in to apply, not on the way 
> out, so no.

This may be a naive example, as I don't work in this domain, but based
upon reviewing the online help at:

  http://finzi.psych.upenn.edu/R/library/genetics/html/genotype.html

and presuming that the intent of the code above is referenced by the
first bullet in the Details section of the function, would the following
work?

This presumes that 'chr1' is a data frame or can be coerced to one as
in:

  chr1 <- as.data.frame(chr1)

Thus:

  data.frame(lapply(subset(chr1, names$breed == 'lab'), 
                    as.genotype, sep =""))

HTH,

Marc Schwartz




More information about the R-help mailing list