I apologize if there is an answer out there already for this... I looked but could not find one.
I am trying to convert a matrix of factors into a matrix of numbers that corresponds to each of the factor values for the column. Simple, right? Yet I have run into a variety of very odd problems when I try to do this.
Let me explain. Here is a sample dataset:
demodata2 <- matrix(c("A","B","B","C",NA,"A","B","B",NA,"C","A","B",NA,"B",NA,"C","A","B",NA,NA,NA,"B","C","A","B","B",NA,"B","B",NA,"B","B",NA,"C","A",NA), nrow=6, ncol=6)
democolnames <- c("Q","R","S","T","U","W")
colnames(demodata2) <- democolnames
Yielding:
Q R S T U W
[1,] "A" "B" NA NA "B" "B"
[2,] "B" "B" "B" NA "B" "B"
[3,] "B" NA NA NA NA NA
[4,] "C" "C" "C" "B" "B" "C"
[5,] NA "A" "A" "C" "B" "A"
[6,] "A" "B" "B" "A" NA NA
Ok. So what I want is this:
Q R S T U W
1 1 2 <NA> <NA> 1 2
2 2 2 2 <NA> 1 2
3 2 <NA> <NA> <NA> <NA> <NA>
4 3 3 3 2 1 3
5 <NA> 1 1 3 1 1
6 1 2 2 1 <NA> <NA>
No problem. Let's try as.numeric(demodata2)
> as.numeric(demodata2)
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[30] NA NA NA NA NA NA NA
Warning message:
NAs introduced by coercion
Less than satisfying. Let's try only one column...
> as.numeric(demodata2[,3])
[1] NA NA NA NA NA NA
Warning message:
NAs introduced by coercion
* edit *
These are actually supposed to be factors, not characters (thanks @Carl Witthoft and @smci)... so let's make this into a dataframe...
> demodata2 <- as.data.frame(demodata2)
> as.numeric(demodata2)
Error: (list) object cannot be coerced to type 'double'
Nope. But wait... here's where it gets interesting...
> as.numeric(demodata2$S)
[1] NA 2 NA 3 1 2
Well, that is right. Let's validate I can do this calling columns by number:
> as.numeric(demodata2[,3])
[1] NA 2 NA 3 1 2
Ok. So I can do this column by column assembling my new matrix by iterating through ncol
times... but is there a better way?
And why does it barf when it is in matrix form, as opposed to data frame? <- edit actually, this is now pretty obvious... in the matrix form, these are characters, not factors. My bad. Question still stands about the dataframe, though...
Thanks! (and pointing me to an existing answer is totally fine)