Displaying 1 result from an estimated 1 matches for "function_returning_a_matrix".
2004 Dec 05
1
matrix of 1,0's to a data.frame of factors
...a data.frame where each column of the matrix becomes
a factor variable.
Now, some columns of the matrix have only 1's or only 0's as a result
there is only 1 level for those columns in the data.frame. However it is
required that each factor have 2 levels. So my solution is:
m <- function_returning_a_matrix()
n <- data.frame( apply(m,2,as.character) )
for (i in 1:ncol(n)) {
levels(n[,i]) <- c(1,0)
}
When m is 118 x 1024 the loop becomes very slow. So I then tried
n <- data.frame( apply(m,2,as.character) )
apply(n,2,function(x) {levels(x) <- c(1,0)})
But t...