Sorry if this is a FAQ. Is there a good reason why a factor has to be a one-dimensional vector and cannot be a matrix? I want to construct matrices of categorical values. Vain attempts like matrix(factor(c(T,F,F,T), 2,2) yield a matrix of character strings representing the factor levels, not the levels themselves, while factor(matrix(c(T,F,F,T), 2,2)) converts the matrix to a logical vector of length 4 then converts the vector to a factor. Tia --- Adrian Baddeley
I'm not sure, but is this what you want matrix(as.numeric(factor(c(T,F,F,T))), 2,2) Tom Mulholland> -----Original Message----- > From: Adrian Baddeley [mailto:adrian at maths.uwa.edu.au] > Sent: Friday, 3 December 2004 1:45 PM > To: r-help at stat.math.ethz.ch > Subject: [R] factor matrix > > > Sorry if this is a FAQ. > > Is there a good reason why a factor has to be > a one-dimensional vector and cannot be a matrix? > > I want to construct matrices of categorical values. > > Vain attempts like > matrix(factor(c(T,F,F,T), 2,2) > yield a matrix of character strings representing the factor levels, > not the levels themselves, while > factor(matrix(c(T,F,F,T), 2,2)) > converts the matrix to a logical vector of length 4 > then converts the vector to a factor. > > Tia > --- > Adrian Baddeley > > ______________________________________________ > 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 >
> matrix(as.numeric(factor(c(T,F,F,T))), 2,2)No, this produces a matrix with numeric values, not categorical values.
This is my playing about The last one is a matrix of factors of dimension 2 by 2. It;s just that it does not look that way.> tt <- matrix(as.numeric(factor(c(T,F,F,T))), 2,2) > str(tt)num [1:2, 1:2] 2 1 1 2> tt <- matrix((factor(c(T,F,F,T))), 2,2) > str(tt)chr [1:2, 1:2] "TRUE" "FALSE" "FALSE" "TRUE"> tt <- c(T,F,F,T) > str(tt)logi [1:4] TRUE FALSE FALSE TRUE> dim(tt) <- c(2,2) > str(tt)logi [1:2, 1:2] TRUE FALSE FALSE TRUE> tt <- factor(c(T,F,F,T)) > dim(tt) <- c(2,2) > str(tt)factor [1:2, 1:2] TRUE FALSE FALSE TRUE - attr(*, "levels")= chr [1:2] "FALSE" "TRUE" - attr(*, "class")= chr "factor"> tt[1] TRUE FALSE FALSE TRUE Levels: FALSE TRUE>> -----Original Message----- > From: Adrian Baddeley [mailto:adrian at maths.uwa.edu.au] > Sent: Friday, 3 December 2004 2:44 PM > To: Mulholland, Tom > Cc: r-help at stat.math.ethz.ch > Subject: RE: [R] factor matrix > > > > > matrix(as.numeric(factor(c(T,F,F,T))), 2,2) > > No, this produces a matrix with numeric values, > not categorical values. > > >