[R] How to avoid automatic coercion to factor?
Marc Schwartz
MSchwartz at medanalytics.com
Wed Sep 3 01:21:05 CEST 2003
On Tue, 2003-09-02 at 17:30, Dutky, Steve wrote:
> I have a function that manipulates a list of numeric and character
> components of equal length and wants to return a data.frame.
>
> EG,
>
> f<-function() {
> a<-list(Int1=1:5,Char1=letters[1:5],Char2=letters[6:10])
> b<-data.frame(a)
> }
>
> How can I get the columns Char1, Char2, (...CharN) returned coerced to
> character and not factor?
>
> It appears that I could coerce individual columns by
> b$CharI<-as.character(b$CharI). Is there a less ugly way of doing this?
>
> Thanks, Steve Dutky
Steve,
Try this:
f<-function() {
a <- list(Int1 = 1:5, Char1 = letters[1:5], Char2 = letters[6:10])
b <- data.frame(I(a))
}
and note the use of I(a) to inhibit the conversion of character vectors
to factors.
I presume that you are doing more in your function than what you have
above...
See ?data.frame and ?I (AsIs) for additional information.
HTH,
Marc Schwartz
More information about the R-help
mailing list