Christian Gold
2007-Nov-13 17:55 UTC
[R] building a data.frame from a matrix: preventing conversion to factors
Dear all: I am trying to build a data.frame from some vectors and some matrices and seem to be unable to find out how to do this without converting everything to factors. I can prevent conversion of the vectors by using I(), as explained in help(data.frame). However, this doesn't help with the matrices: if I try I() with the matrix, the matrix gets incorporated into the data frame as a whole, rather than as a list of vectors as it should be. ("as.is=T" doesn't help either, and neither does "options(stringsAsFactors = FALSE)".) Any help would be greatly appreciated! Christian Gold
Prof Brian Ripley
2007-Nov-13 20:46 UTC
[R] building a data.frame from a matrix: preventing conversion to factors
I am trying to build a data.frame from some vectors and some matrices and seem to be unable to find out how to do this without converting everything to factors. I can prevent conversion of the vectors by using I(), as explained in help(data.frame). However, this doesn't help with the matrices: if I try I() with the matrix, the matrix gets incorporated into the data frame as a whole, rather than as a list of vectors as it should be. ("as.is=T" doesn't help either, and neither does "options(stringsAsFactors = FALSE)".) I think we need the reproducible example we asked for. I get> z <- matrix(letters[1:12], 4, 3) > z2 <- data.frame(x=1:4, z=z, stringsAsFactors=FALSE) > sapply(z2, class)x z.1 z.2 z.3 "integer" "character" "character" "character" so it is working as documented. (BTW, using I() on a matrix is intended to insert a matrix as a single entity, and as.is is not an argument to data.frame.) -- 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
Seemingly Similar Threads
- How to avoid conversion to factors (data frame to zoo)
- aggregate.data.frame - prevent conversion to factors? show statistics for NA values of "by" variable?
- converting the string columns in a data.frame to factors?
- Selecting cases from matrices stored in lists
- multi-column factor