[R] retaining character matrices when combining into data frames

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 16 08:35:29 CEST 2006


On Mon, 15 May 2006, Satoshi Takahama 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.

Which is?  Your subject line says you want to retain a character matrix, 
and that is exactly what happens:

> g <- matrix(letters[1:4], 2, 2)
> as.data.frame(I(g))
   x.1 x.2
1   a   c
2   b   d

> What I hope to retrieve is the result of
>
> h = data.frame(I(g[,1]),I(g[,2]))
> names(h) = dimnames(g)[[2]]

So it seems you don't actually want to `retain character matrices'.

> 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().

Really?  How are you trying to do it?

> 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.

It was not there in R 1.0.0, so you are talking about alpha/beta versions.
I think it existed only when data.frame was a rather different class.

> Is there a more attractive alternative available now?

I guess what you want is for each column of a character matrix to be 
inserted as a character vector.  There are several ways: a simple one is

DF <- data.frame(g)
for(i in 1:ncol(g)) DF[[i]] <- g[, i]

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list