[R] retaining character matrices when combining into data frames

Gabor Grothendieck ggrothendieck at gmail.com
Tue May 16 03:59:49 CEST 2006


Try this:

# test data
mat <- matrix(letters, 2)

# convert to data frame with character columns
DF <- replace(as.data.frame(mat),,mat)

On 5/15/06, Satoshi Takahama <stakahama at ucsd.edu> wrote:
> Hello everyone,
>
> If I want to convert or combine a (large) character matrix into a data
> frame without having any of its columns convert into a factor class,
> is there a simple solution? I() says it will operate on 'an
> object' but it seems that unless the object is a vector, the results
> are not what I expect.
>
> For instance, if g is a 2x2 character matrix, as.data.frame(I(g)) will
> return an object of the data frame class but not structured in the way
> I intended. What I hope to retrieve is the result of
>
> h = data.frame(I(g[,1]),I(g[,2]))
> names(h) = dimnames(g)[[2]]
>
> With data sets of 100+ columns, wrapping I() around each column can be
> very time-consuming when invoking the functions data.frame(), cbind(),
> or as.data.frame(). R used to offer the optional argument, as.is=TRUE
> (and S-PLUS offered stringsAsFactors=FALSE), to accomplish what this
> task but it seems that this argument was removed some time ago. Is
> there a more attractive alternative available now?
>
> Thanks very much,
>
> Satoshi
>
>
> __________
> Satoshi Takahama
> Scripps Institution of Oceanography
> Center for Atmospheric Science
> 9500 Gilman Drive, Dept. 0221
> La Jolla, CA 92093
> 858-531-5328
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list